天天看点

C#使用post提交http请求

版权声明:欢迎评论和转载,转载请注明来源。 https://blog.csdn.net/zy332719794/article/details/50517403

string strId = "guest";
  string strPassword= "123456";

  ASCIIEncoding encoding=new ASCIIEncoding();
  string postData="userid="+strId;
  postData += ("&password="+strPassword);

  byte[] data = encoding.GetBytes(postData);

  // Prepare web request...
  HttpWebRequest myRequest =
   (HttpWebRequest)WebRequest.Create("http://www.here.com/login.asp");

  myRequest.Method = "POST";
  myRequest.ContentType="application/x-www-form-urlencoded";
  myRequest.ContentLength = data.Length;
  Stream newStream=myRequest.GetRequestStream();

  // Send the data.
  newStream.Write(data,0,data.Length);
  newStream.Close();

  // Get response
  HttpWebResponse myResponse=(HttpWebResponse)myRequest.GetResponse();
  StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.Default);
  string content = reader.ReadToEnd();
  Console.WriteLine(content);