首页 > 代码库 > http请求示例

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

                       }

}

 

http请求示例