码迷,mamicode.com
首页 > Web开发 > 详细

http请求示例

时间:2014-12-23 11:47:53      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

  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();     

                       }

}

 

http请求示例

标签:

原文地址:http://www.cnblogs.com/apecblue/p/4179564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!