Skip to content

Instantly share code, notes, and snippets.

@paulvonlecter
Created March 30, 2017 07:02
Show Gist options
  • Save paulvonlecter/cacf0229c8f434efe10eb5ffd5a59b8c to your computer and use it in GitHub Desktop.
Save paulvonlecter/cacf0229c8f434efe10eb5ffd5a59b8c to your computer and use it in GitHub Desktop.
GET & POST requests' functions in C#
private static string POST(string Url, string Data) {
System.Net.WebRequest req = System.Net.WebRequest.Create(Url);
req.Method = "POST";
req.Timeout = 100000;
req.ContentType = "application/x-www-form-urlencoded";
// Кодировка указывается в зависимости от кодировки клиента
byte[] sentData = Encoding.GetEncoding(1251).GetBytes(Data);
req.ContentLength = sentData.Length;
System.IO.Stream sendStream = req.GetRequestStream();
sendStream.Write(sentData, 0, sentData.Length);
sendStream.Close();
System.Net.WebResponse res = req.GetResponse();
System.IO.Stream ReceiveStream = res.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(ReceiveStream, Encoding.UTF8);
// Кодировка указывается в зависимости от кодировки ответа сервера
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
string Out = String.Empty;
while (count > 0) {
String str = new String(read, 0, count);
Out += str;
count = sr.Read(read, 0, 256);
}
return Out;
}
private static string GET(string Url, string Data) {
System.Net.WebRequest req = System.Net.WebRequest.Create(Url + "?" + Data);
System.Net.WebResponse resp = req.GetResponse();
System.IO.Stream stream = resp.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(stream);
string Out = sr.ReadToEnd();
sr.Close();
return Out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment