Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Get Запрос
- public string SendGet()
- {
- try
- {
- var httpWebRequest = (HttpWebRequest) WebRequest.Create("http://site.com");
- httpWebRequest.AllowAutoRedirect = false; //Запрещаем автоматический redirect
- httpWebRequest.Method = "GET"; //Можно не указывать, по умолчанию используется GET.
- httpWebRequest.Referer = "https://rdot.org";
- httpWebRequest.UserAgent = UserAgent;
- using (var httpWebResponse = (HttpWebResponse) httpWebRequest.GetResponse())
- {
- using (var stream = httpWebResponse.GetResponseStream())
- {
- using (var reader = new StreamReader(stream,Encoding.GetEncoding(httpWebResponse.CharacterSet)))
- { return reader.ReadToEnd();}
- }
- }
- }
- catch
- {
- return null;
- }
- }
- Post запрос
- public string SendPost(string postData)
- {
- var httpWebRequest = (HttpWebRequest) WebRequest.Create("http://site.com");
- httpWebRequest.AllowAutoRedirect = false;
- httpWebRequest.Method = "POST";
- httpWebRequest.ContentType = "application/x-www-form-urlencoded";
- var buffer = Encoding.ASCII.GetBytes(postData);
- httpWebRequest.ContentLength = buffer.Length;
- using (var writer = httpWebRequest.GetRequestStream())
- { writer.Write(buffer, 0, buffer.Length); }
- using (var httpWebResponse = (HttpWebResponse) httpWebRequest.GetResponse())
- {
- using (var stream = httpWebResponse.GetResponseStream())
- {
- using (var reader = new StreamReader(stream, Encoding.GetEncoding(httpWebResponse.CharacterSet)))
- { return reader.ReadToEnd(); }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement