I need to read the content of a webpage in streamreader like
www.example.com
<test>
<sample></sample>
</test>
i got this:
System.IO.StreamReader StreamReader1 =
new System.IO.StreamReader("www.example.com");
string test = StreamReader1.ReadToEnd();
but i then i get this error code
Attempt to access the method failed:
System.IO.StreamReader..ctor(System.String)
Try a WebClient, it's easier and you don't have to worry about streams and rivers:
using (var client = new WebClient())
{
string result = client.DownloadString("http://www.example.com");
// TODO: do something with the downloaded result from the remote
// web site
}
If you want to use the StreamReader, here is the code I am using:
const int Buffer_Size = 100 * 1024;
WebRequest request = CreateWebRequest(uri);
WebResponse response = request.GetResponse();
result = GetPageHtml(response);
...
private string GetPageHtml(WebResponse response) {
char[] buffer = new char[Buffer_Size];
Stream responseStream = response.GetResponseStream();
using(StreamReader reader = new StreamReader(responseStream)) {
int index = 0;
int readByte = 0;
do {
readByte = reader.Read(buffer, index, 256);
index += readByte;
}
while (readByte != 0);
response.Close();
}
string result = new string(buffer);
result = result.TrimEnd(new char[] {'\0'});
return result;
}
Related
Want to Decompress a Response which is GZipped Getting from an API.Tried the Below Code ,It Always return Like:-
\u001f�\b\0\0\0\0\0\0\0�Y]o........
My code is:
private string GetResponse(string sData, string sUrl)
{
try
{
string script = null;
try
{
string urlStr = #"" + sUrl + "?param=" + sData;
Uri url = new Uri(urlStr, UriKind.Absolute);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
script = reader.ReadToEnd();
}
}
catch (System.Net.Sockets.SocketException)
{
// The remote site is currently down. Try again next time.
}
catch (UriFormatException)
{
// Only valid absolute URLs are accepted
}
return script;
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
I Found the Above Code from many References for Automatic Decompression.But Eventually,it doesn't Work for me.So as to Unzip the zipped Data I tried the Below Function,
private string DecompressGZIP(string compressedText)
{
byte[] gZipBuffer = Convert.FromBase64String(compressedText);
using (var memoryStream = new MemoryStream())
{
int dataLength = BitConverter.ToInt32(gZipBuffer, 0);
memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);
var buffer = new byte[dataLength];
memoryStream.Position = 0;
using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
gZipStream.Read(buffer, 0, buffer.Length);
}
return Encoding.UTF8.GetString(buffer);
}
}
But,it also Failed in the First Line of code itself Because of the Following Exception:
System.FormatException: 'The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. '
As i am a Beginner,Hope You guys will Guide Me .....Thanks in advance....
This is the essential bit which will take care of decoding the gzipped stream:
var clientHandler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate };
var client = new HttpClient(clientHandler);
Just Change My Function as Follows,Which is Perfectly Working For me:
private JObject PostingToPKFAndDecompress(string sData, string sUrl)
{
var jOBj = new JObject();
try
{
try
{
string urlStr = #"" + sUrl + "?param=" + sData;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlStr);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
var t = ReadFully(resStream);
var y = Decompress(t);
using (var ms = new MemoryStream(y))
using (var streamReader = new StreamReader(ms))
using (var jsonReader = new JsonTextReader(streamReader))
{
jOBj = (JObject)JToken.ReadFrom(jsonReader);
}
}
catch (System.Net.Sockets.SocketException)
{
// The remote site is currently down. Try again next time.
}
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
return jOBj;
}
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
public static byte[] Decompress(byte[] data)
{
using (var compressedStream = new MemoryStream(data))
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
using (var resultStream = new MemoryStream())
{
zipStream.CopyTo(resultStream);
return resultStream.ToArray();
}
}
I tried to create message with custom keyboard. So I send request with
reply_markup = {"keyboard":[["1"],["2"]],"resize_keyboard":"True","one_time_keyboard":"True"}
But, it does not work.
I tried all of Content-Types:
application/x-www-form-urlencoded (create message with default
keyboard)
application/json (create message with default keyboard)
multipart/form-data (does not work at all, inspite of this Post)
I also tried to send message by 2 different ways. What's the wrong with this code?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
namespace DutyReminder
{
class Program
{
static void Main(string[] args)
{
string message = "message";
string message1 = "message1";
string botid = "165749848:AAGtjn42bajF-WxdKosTF07sLwJPYlqiDZE";
string chatid = "38651047";
Sender.send("", "https://api.telegram.org/bot" + botid + "/sendmessage?chat_id=" + chatid + "&text=" + message + "&reply_markup={\"keyboard\":[[\"1\"],[\"2\"]],\"resize_keyboard\":\"True\",\"one_time_keyboard\":\"True\"}");
Sender.HttpPost("https://api.telegram.org/bot" + botid + "/sendmessage?chat_id=" + chatid + "&text=" + message1 + "&reply_markup={\"keyboard\":[[\"1\"],[\"2\"]],\"resize_keyboard\":\"True\",\"one_time_keyboard\":\"True\"}", "");
}
}
static class Sender
{
static public void send(string message, string url)
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(url);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
//string postData = "{\"value1\":\"" + message + "\"}";
string postData = message;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
// request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
}
static public string HttpPost(string URI, string Parameters)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
// req.Proxy = new System.Net.WebProxy(ProxyString, true);
//Add these, as we're doing a POST
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
//We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value&
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
req.ContentLength = bytes.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length); //Push it out there
os.Close();
System.Net.WebResponse resp = req.GetResponse();
if (resp == null) return null;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}
}
}
Use the following instead of URI message sending:
var bot = new Api("YourApiToken");
.
.
.
var rmu = new ReplyKeyboardMarkup();
rmu.Keyboard =
new string[][]
{
new string[] {"1-1", "1-2"},
new string[] {"2"},
new string[] {"3-1", "3-2" , "3-3" }
};
await bot.SendTextMessage(update.Message.Chat.Id, msg, false, 0, rmu);
UPDATED
With the newer version of API:
var bot = new Api("YourApiToken");
.
.
.
var rkm = new ReplyKeyboardMarkup();
rkm.Keyboard =
new KeyboardButton[][]
{
new KeyboardButton[]
{
new KeyboardButton("1-1"),
new KeyboardButton("1-2")
},
new KeyboardButton[]
{
new KeyboardButton("2")
},
new KeyboardButton[]
{
new KeyboardButton("3-1"),
new KeyboardButton("3-2"),
new KeyboardButton("3-3")
}
};
await bot.SendTextMessage(update.Message.Chat.Id, msg, false, false, 0, rkm );
You can dynamicly create keyboard.
var rkm = new ReplyKeyboardMarkup();
var rows = new List<KeyboardButton[]>();
var cols = new List<KeyboardButton>();
for (var Index = 1; Index < 17; Index++)
{
cols.Add(new KeyboardButton("" + Index));
if (Index%4 != 0) continue;
rows.Add(cols.ToArray());
cols = new List<KeyboardButton>();
}
rkm.Keyboard = rows.ToArray();
await botClient.SendTextMessageAsync(
message.Chat.Id,
"Choose",
replyMarkup: rkm);
I did it my way :)
public static class KeyboardHelper
{
public static ReplyKeyboardMarkup GetKeyboard(List<string> keys)
{
var rkm = new ReplyKeyboardMarkup();
var rows = new List<KeyboardButton[]>();
var cols = new List<KeyboardButton>();
foreach (var t in keys)
{
cols.Add(new KeyboardButton(t));
rows.Add(cols.ToArray());
cols = new List<KeyboardButton>();
}
rkm.Keyboard = rows.ToArray();
return rkm;
}
}
List<string> items = new List<string>();
items.Add("A");
items.Add("B");
items.Add("C");
items.Add("D");
items.Add("E");
var rkm = new ReplyKeyboardMarkup();
var rows = new List<KeyboardButton[]>();
var cols = new List<KeyboardButton>();
for (var Index = 1; Index < items.Count+1; Index++)
{
cols.Add(new KeyboardButton("" + items[Index-1]));
if (Index % 4 != 0) continue;
rows.Add(cols.ToArray());
cols = new List<KeyboardButton>();
}
if (cols.Count > 0) {rows.Add(cols.ToArray());}
rkm.Keyboard = rows.ToArray();
await Bot.SendTextMessageAsync(message.Chat.Id,"Choose",replyMarkup: rkm);
I got code, that sending GET request and recieves answer in stream. I read stream with streamreader to end. Here is code:
HttpWebRequest requestGet = (HttpWebRequest)WebRequest.Create(url);
requestGet.Method = "GET";
requestGet.Timeout = 5000;
HttpWebResponse responseGet = (HttpWebResponse)requestGet.GetResponse();
StreamReader reader = new StreamReader(responseGet.GetResponseStream());
StringBuilder output = new StringBuilder();
output.Append(reader.ReadToEnd());
responseGet.Close();
But i dont like that program is waiting until all data recieved before starting working with response. It would be great if i can do it like this(pseudocode):
//here sending GET request
do
{
response.append(streamPart recieved);
//here work with response
} while (stream not ended)
I tried streamReader.Read(char[], int32_1, int32_2), but i cant specify int32_2, becouse i dont know how many symbols i recieved. And if i use ReadToEnd - it waits for all response to load.
Read takes a char[] buffer and returns the number of characters read from the stream.
Here's an example of how to read the response in chunks:
public static void Main(string[] args)
{
var req = WebRequest.Create("http://www.google.com");
req.Method = "GET";
req.Timeout = 5000;
using (var response = req.GetResponse())
using (var reader = new StreamReader(response.GetResponseStream()))
{
char[] buffer = new char[1024];
int read = 0;
int i = 0;
do
{
read = reader.Read(buffer, 0, buffer.Length);
Console.WriteLine("{0}: Read {1} bytes", i++, read);
Console.WriteLine("'{0}'", new String(buffer, 0, read));
Console.WriteLine();
} while(!reader.EndOfStream);
}
}
I didn't see any extraneous white space or repeated data.
I am trying to upload file using httpwebrequest and rest api
My code is like this i am not getting any error but i cannt able to upload file.
public static string RequestProfileUrl = "https://ws.onehub.com/workspaces/337426/folders/174352646/files/create?items[filename]=";
if (file != null && file.ContentLength > 0)
{
byte[] bytearray = null;
string name = "";
long length = 0;
string boundary = "----------------------------" +
DateTime.Now.Ticks.ToString("x");
name = file.FileName;
Stream stream = file.InputStream;
stream.Seek(0, SeekOrigin.Begin);
bytearray = new byte[stream.Length];
int count = 0;
Stream memStream = new System.IO.MemoryStream();
while (count < stream.Length)
{
bytearray[count++] = Convert.ToByte(stream.ReadByte());
}
//string baseAddress = "https://ws-api.onehub.com/workspaces/330201/files/";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestProfileUrl + name);
request.Method = "PUT";
request.ContentType = "multipart/form-data";
request.ContentLength = bytearray.Length;
request.GetRequestStream().Write(bytearray, 0, bytearray.Length);
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
int statusCode = (int)response.StatusCode;
StreamReader reader = new StreamReader(response.GetResponseStream());
string result = reader.ReadToEnd();
}
i am not getting any error but my file is not uploading. i am not getting where is my error?
any question free to ask
Thanks in advance
Does not recognize the uncoding what to do?
var url = "http://translate.google.ru/translate_a/t?client=x&text=" + text + "&hl=en&sl=en&tl=ru";
new System.Net.WebClient().DownloadFile(url, "filePath");
StreamReader streamReader = new StreamReader("filePath", Encoding.UTF8);
string trn = streamReader.ReadToEnd();
streamReader.Close();
return trn;
Label1.Text = tr.GoogleTranslate("testers");
Result: �������
Here how you can read that using the WebRequest
HttpWebResponse response = null;
StreamReader reader = null;
Stream dataStream = null;
StringBuilder sbReadBuffer = null;
int bufSizeMin = 8192;
int bufSizeMax = 65536;
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(TheWebPageToRead);
if (req != null)
{
req.Method = "GET";
response = (HttpWebResponse)req.GetResponse();
if (response != null)
{
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
reader = new StreamReader(dataStream, true);
// get the length of the content returned by the request
int length = (int)response.ContentLength;
int bufSize = bufSizeMin;
if (length > bufSize)
bufSize = length > bufSizeMax ? bufSizeMax : length;
// allocate buffer and StringBuilder for reading response
byte[] buf = new byte[bufSize];
sbReadBuffer = new StringBuilder(bufSize);
// read the whole response
while ((length = dataStream.Read(buf, 0, buf.Length)) != 0){
sbReadBuffer.Append(Encoding.UTF8.GetString(buf, 0, length));
}
}
}
}
finally
{
if (response != null)
response.Close();
if (reader != null)
reader.Close();
if (dataStream != null)
dataStream.Close();
}
The TheWebPageToRead is yours url.
The sbReadBuffer keeps the return and asking for "testers" I get:
{"sentences":[{"trans":"Тестеры","orig":"testers","translit":"Testery","src_translit":""}],"src":"en","server_time":11}
asking for "aristos", I get: {"sentences":[{"trans":"аристократов","orig":"aristos","translit":"aristokratov","src_translit":""}],"src":"en","server_time":5}