A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© sunrise2 高级黑马   /  2014-8-11 09:16  /  548 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

封装了WEBCLIENT HttpWebRequest 两中方法进行http的处理
当前只写了get post;且对于302的跳转是开启的
  
  1. namespace webclient
  2. {
  3.     class httpclient
  4.     {
  5.         private string cookie;
  6.         public string Cookie
  7.         {
  8.             get { return cookie; }
  9.             set { cookie = value; }
  10.         }

  11.         private string referer;
  12.         public string Referer
  13.         {
  14.             get { return referer; }
  15.             set { referer = value; }
  16.         }

  17.         /// <summary>
  18.         ///  WEBCLIENT 方法处理http 当postdata为空  就使用GET,不为空就使用 POST
  19.         /// </summary>
  20.         /// <param name="url"></param>
  21.         /// <param name="postdata"></param>
  22.         /// <param name="type"></param>
  23.         /// <returns></returns>
  24.         public string webclient(string url, string postdata, string type)
  25.         {
  26.             string ret = "";
  27.             WebClient myClient = new WebClient();
  28.             myClient.Encoding = Encoding.GetEncoding(type);
  29.             myClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
  30.             myClient.Headers.Add("Cookie", cookie);
  31.             myClient.Headers.Add("Referer", referer);
  32.             if (postdata.Length == 0)
  33.             {
  34.                 ret = myClient.DownloadString(url);
  35.             }
  36.             else
  37.             {
  38.                 ret = myClient.UploadString(url, postdata);
  39.             }            
  40.             if (cookie.Length == 0 )
  41.             {
  42.                 cookie = myClient.ResponseHeaders.Get("Set-Cookie");
  43.             }
  44.             return ret;
  45.         }

  46.         /// <summary>
  47.         /// HttpWebRequest 方法处理http 当postdata为空  就使用GET,不为空就使用 POST
  48.         /// </summary>
  49.         /// <param name="url"></param>
  50.         /// <param name="postdata"></param>
  51.         /// <param name="type"></param>
  52.         /// <returns></returns>
  53.         public string HttpWebResponse(string url, string postdata, string type)
  54.         {
  55.             string ret = "";
  56.             HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));           
  57.             myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
  58.             myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
  59.             myHttpWebRequest.AllowAutoRedirect = true;           
  60.             //myHttpWebRequest.co
  61.             /*-----  */
  62.             string[] arrCookie = cookie.Split(';');
  63.             CookieContainer cookie_container = new CookieContainer();    //加载Cookie            
  64.             foreach (string sCookie in arrCookie)
  65.             {
  66.                 if (sCookie.IndexOf("expires") > 0)
  67.                     continue;
  68.                 cookie_container.SetCookies(new Uri(url), sCookie);
  69.             }
  70.             myHttpWebRequest.CookieContainer = cookie_container;
  71.             /*-----  */           
  72.             myHttpWebRequest.Referer = referer;
  73.             if (postdata.Length != 0)
  74.             {
  75.                 myHttpWebRequest.Method = "POST";
  76.                 myHttpWebRequest.ContentLength = postdata.Length;
  77.                 Stream newStream = myHttpWebRequest.GetRequestStream();
  78.                 // Send the data.
  79.                 byte[] byteArray = Encoding.GetEncoding(type).GetBytes(postdata);
  80.                 newStream.Write(byteArray, 0, byteArray.Length);    //写入参数
  81.                 newStream.Close();
  82.                 HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();               
  83.                 StreamReader str = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(type));
  84.                 ret = str.ReadToEnd();
  85.                 cookie = response.Headers.Get("Set-Cookie");
  86.                 str.Close();
  87.                 response.Close();
  88.               
  89.             }
  90.             else
  91.             {  
  92.                 myHttpWebRequest.Method = "GET";
  93.                 HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();
  94.                 Stream responseStream = response.GetResponseStream();
  95.                 StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding(type));
  96.                 ret = sReader.ReadToEnd();
  97.                 cookie = response.Headers.Get("Set-Cookie");
  98.                 sReader.Close();
  99.                 response.Close();
  100.                  
  101.             }            
  102.                           
  103.             return ret;
  104.         }
  105.   
  106.     }
  107. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马