zoukankan      html  css  js  c++  java
  • 模拟浏览器请求URL 带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;
    }


  • 相关阅读:
    [网站设计]网站设计的流程
    教你几招如何看透一个人
    难得迷茫
    java 日期 加减 运算
    第01章 SQL Server数据库基础 读后感
    [网站设计]如何设计一个成功的网站
    [网站设计] 素材网罗
    转载个人毕业5年职业感想
    SWTDesigner
    [存档]asp.net夜话之十一:web.config详解收藏
  • 原文地址:https://www.cnblogs.com/stangray/p/1733972.html
Copyright © 2011-2022 走看看