http请求示例

  Stream sw = null;
   Stream res = null;
 StreamWriter myWriter;
 HttpWebResponse rsp = null;

  string queryUrl = url + "?" + queryString;

 try {                

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(queryUrl);                

req.Method = "POST";                

req.ContentType = "application/x-www-form-urlencoded";

sw = req.GetRequestStream();                

myWriter = new StreamWriter(sw);                

myWriter.Write(queryString);                

myWriter.Close();                

myWriter.Dispose();

  rsp = (HttpWebResponse)req.GetResponse();
  res = rsp.GetResponseStream();
 var buff = new byte[4096];

 if (res != null)
                {

    int resLen = res.Read(buff, 0, buff.Length);
    string str = Encoding.UTF8.GetString(buff, 0, resLen).Trim(‘\0‘).Trim(‘\n‘);

   catch (Exception err)             {

               }           

  finally            

{                

if (res != null) res.Dispose();    

             if (rsp != null) rsp.Close();        

         if (sw != null) sw.Dispose();     

                       }

}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。