This article is a mirror article of machine translation, please click here to jump to the original article.

View: 20787|Reply: 2

[Source] C# uses GET, POST requests to get results

[Copy link]
Posted on 12/10/2015 11:13:13 AM | | | |


I just show the result after the get request as above:

The code is as follows:

httpget request:

  1. /// <summary>
  2. /// GET请求与获取结果
  3. /// </summary>
  4. public static string HttpGet(string Url, string postDataStr)
  5. {
  6.     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
  7.     request.Method = "GET";
  8.     request.ContentType = "text/html;charset=UTF-8";

  9.     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  10.     Stream myResponseStream = response.GetResponseStream();
  11.     StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
  12.     string retString = myStreamReader.ReadToEnd();
  13.     myStreamReader.Close();
  14.     myResponseStream.Close();

  15.     return retString;
  16. }
Copy code
Call get request:
  1. static void Main(string[] args)
  2. {
  3.     string url = "http://www.itsvse.com/LoginHandler.aspx";
  4.     string data = "UserName=admin&Password=123";
  5.     string result = HttpGet(url, data);
  6.     Console.WriteLine(result);
  7.     Console.ReadLine();
  8. }
Copy code



httppost request:

  1. /// <summary>
  2. /// POST请求与获取结果
  3. /// </summary>
  4. public static string HttpPost(string Url, string postDataStr)
  5. {
  6.     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
  7.     request.Method = "POST";
  8.     request.ContentType = "application/x-www-form-urlencoded";
  9.     request.ContentLength = postDataStr.Length;
  10.     StreamWriter writer = new StreamWriter(request.GetRequestStream(),Encoding.ASCII);
  11.     writer.Write(postDataStr);
  12.     writer.Flush();
  13.     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  14.     string encoding = response.ContentEncoding;
  15.     if (encoding == null || encoding.Length < 1) {
  16.         encoding = "UTF-8"; //默认编码
  17.     }
  18.     StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
  19.     string retString = reader.ReadToEnd();
  20.     return retString;
  21. }
Copy code


Calling the post request:

  1. static void Main(string[] args)
  2. {
  3.     string url = "http://www.itsvse.com/LoginHandler.aspx";
  4.     string data = "UserName=admin&Password=123";
  5.     string result = HttpPost(url, data);
  6.     Console.WriteLine(result);
  7.     Console.ReadLine();
  8. }
Copy code






Previous:Tip: How to set up VS2010 to start multiple projects at the same time
Next:How to reference pointer variables and compare sizes
Posted on 12/11/2015 2:24:17 PM |
your lungs
 Landlord| Posted on 3/11/2016 3:27:08 PM |
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com