点击这里给我发消息 点击这里给我发消息

HttpWebRequest实现浏览器请求(Cookie)

添加时间:2013-12-6
    相关阅读: Windows

 private CookieContainer cookieContainer =  = new CookieContainer();

  public string RequestByPost(string url, string postData)

  {

  int num = 3;

  string str = null;

  while (num-- > 0)

  {

  try

  {

  Thread.Sleep((int)(delayTime * 0x3e8));

  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

  request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)";

  request.Method = "POST";

  request.Accept = "/upload/users/1/20131206/png,*/*;q=0.5";

  request.KeepAlive = true;

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

  request.CookieContainer = this.cookieContainer;

  //request.Proxy = this.webProxy;

  HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);

  request.CachePolicy = policy;

  byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(postData);

  request.ContentLength = bytes.Length;

  Stream requestStream = request.GetRequestStream();

  requestStream.Write(bytes, 0, bytes.Length);

  requestStream.Close();

  HttpWebResponse response = (HttpWebResponse)request.GetResponse();

  StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8, true);

  str = reader.ReadToEnd();

  response.Close();

  reader.Close();

  return str;

  }

  catch (Exception exception)

  {

  if (exception.Message.IndexOf("内部服务器错误") > 0)

  {

  return "(500)";

  }

  Logging("RequestByPost", exception.Message);

  continue;

  }

  }

  return str;

  }

  public string RequestByGet(string url)

  {

  int num = 3;

  string str = null;

  while (num-- > 0)

  {

  try

  {

  Thread.Sleep((int)(delayTime * 0x3e8));

  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

  request.Method = "GET";

  request.CookieContainer = this.cookieContainer;

  HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);

  request.CachePolicy = policy;

  HttpWebResponse response = (HttpWebResponse)request.GetResponse();

  StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8, true);

  str = reader.ReadToEnd();

  response.Close();

  reader.Close();

  return str;

  }

  catch (Exception exception)

  {

  if (exception.Message.IndexOf("内部服务器错误") > 0)

  {

  return "(500)";

  }

  Logging("RequestByGet", exception.Message);

  continue;

  }

  }

  return str;

  }

本文作者:未知
咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册