首页 > 代码库 > 提交服务器 post get

提交服务器 post get

  • HttpRequest Post or Get
  • // method --- WebRequestMethods.Http.Post 或 WebRequestMethods.Http.Getprivate string HttpRequestGet(string url, string method, string data){   var request = WebRequest.Create(url);   request.Method = method;   if (method == WebRequestMethods.Http.Post)   {       byte[] buffer = Encoding.Default.GetBytes(data);       request.ContentType = "application/x-www-form-urlencoded";       request.ContentLength = buffer.Length;       Stream streamRequest = request.GetRequestStream();       streamRequest.Write(buffer, 0, buffer.Length);       streamRequest.Close();   }    var response = request.GetResponse();    var streamResponse = response.GetResponseStream();    if (streamResponse == null)  {     return "";  }    StreamReader reader = new StreamReader(streamResponse, Encoding.Default);    string result = reader.ReadToEnd();    streamResponse.Close();    reader.Close();    return result;}

     

  • WebClient Get
  • public string WebClientGet(string url){    WebClient client = new WebClient();    client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");    Stream stream = client.OpenRead(url);    if (stream == null)   {    return "";  }    StreamReader reader = new StreamReader(stream, Encoding.Default);    string result = reader.ReadToEnd();    stream.Close();    reader.Close();    return result;}

     

  • public string GetStr(string data){  string url = "";  byte[] postData =http://www.mamicode.com/ Encoding.UTF8.GetBytes(data);  WebClient webClient = new WebClient();  webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可   byte[] responseData = http://www.mamicode.com/webClient.UploadData(url, "POST", postData);//得到返回字符流  string getStr = Encoding.UTF8.GetString(responseData);//解码  return getStr;}

     

 

提交服务器 post get