HttpWebRequest实现浏览器请求(Cookie)


 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 = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/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;

  }



相关阅读:
关注于使用常用对象来加快Access 2007数据库下
PHP 工厂模式使用方法
特定的条件下同样可以灵活易用
遇到Windows 7媒体中心看网络视频问题怎样解答
ASP.NET点击按钮弹出确认对话框方法
mysql中判断记录是否存在方法比较
php 高性能书写
让64位的Win Server 2008支持蓝牙
PHP求小于1000的所有水仙花数的代码
在textarea输入Tab
ASP视频教程:备份和还原SQL Server 2000数据库
用css动态生成闪字的代码
ASP 使用Filter函数来检索数组的实现代码
php的正则处理函数总结分析
快速导航

Copyright © 2016 phpStudy | 皖ICP备18014864号-4