HttpWebRequest用法

GET方法
static void Main(string[] args)
{
    string url = "http://www.xxx.com/?param1=value1&param2=value2";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

    request.Method = "GET";
    request.Headers["Accept-Language"] = "zh-Hans-CN,zh-Hans;q=0.5";
    request.Headers["Accept-Encoding"] = "gzip, deflate";
    /*
    如果要获取Cookie
    CookieContainer cookieContainer = new CookieContainer();
    request.CookieContainer = cookieContainer;
    */

    /* 获取返回的网页内容 */
    {
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        /* 此时已通过request.GetResponse()发出请求 */
        StreamReader responseStream = new StreamReader(response.GetResponseStream());
        string text = responseStream.ReadToEnd();
    }
    Console.WriteLine(text);

    request.Abort();
    response.Close();
    responseStream.Close();
}
POST方法
static void Main(string[] args)
{
    string url = "http://www.xxx.com";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    byte[] postdata = Encoding.UTF8.GetBytes("param1=value1&param2=value2");

    request.Method = "POST";
    request.Headers["Accept-Language"] = "zh-Hans-CN,zh-Hans;q=0.5";
    request.Headers["Accept-Encoding"] = "gzip, deflate";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = postdata.Length;
    /*
    如果要获取Cookie
    CookieContainer cookieContainer = new CookieContainer();
    request.CookieContainer = cookieContainer;
    */

    /* 发送POST请求 */
    Stream requestStream = request.GetRequestStream();
    requestStream.Write(postdata, 0, postdata.Length);
    /* 此时已通过HttpWebRequest.GetRequestStream().Write(byte[] buffer, int count)方法发出请求 */

    /* 获取返回的网页内容 */
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader responseStream = new StreamReader(response.GetResponseStream());
    string text = responseStream.ReadToEnd();

    Console.WriteLine(text);

    request.Abort();
    response.Close();
    responseStream.Close();
}
  • 要重复发送请求时需要再次使用(HttpWebRequest)WebRequest.Creat(Uri),重新创建请求实现重复发送.
  • 如果网站使用了GZIP/Deflate压缩,HttpWebRequest会自动解压缩.
使用HTTP代理
static void Main(string[] args)
{
    string url = "http://www.xxx.com/?param1=value1&param2=value2";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

    /* 创建并设置使用代理 */
    WebProxy proxy = new WebProxy("127.0.0.1", 8888);
    request.Proxy = proxy;

    /*
    如果HTTP代理需要账号密码登陆
    proxy.Credentials = new NetworkCredential("UserName", "PassWord");
    */

    /* 获取返回的网页内容 */
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader responseStream = new StreamReader(response.GetResponseStream());
    string text = responseStream.ReadToEnd();

    Console.WriteLine(text);

    request.Abort();
    response.Close();
    responseStream.Close();
}
CookieContainer中Cookie跨域或丢失问题

CookieContainer中已经有所需的Cookie时,使用HttpWebRequest发送请求却没有或缺少Cookies时的两个解决方案.
Here is the solution:
1. 不使用CookieContainer.Add(Cookie)方法,只使用CookieContainer.Add(Uri, Cookie)方法.
2. Call BugFix_CookieDomain each time you add a cookie to the container or before you use .GetCookie or before system use the container.

private void BugFix_CookieDomain(CookieContainer cookieContainer)
{
    System.Type _ContainerType = typeof(CookieContainer);
    Hashtable table = (Hashtable)_ContainerType.InvokeMember("m_domainTable",
                               System.Reflection.BindingFlags.NonPublic |
                               System.Reflection.BindingFlags.GetField |
                               System.Reflection.BindingFlags.Instance,
                               null,
                               cookieContainer,
                               new object[] { });
    ArrayList keys = new ArrayList(table.Keys);
    foreach (string keyObj in keys)
    {
        string key = (keyObj as string);
        if (key[0] == '.')
        {
            string newKey = key.Remove(0, 1);
            table[newKey] = table[keyObj];
        }
    }
}

来源:http://stackoverflow.com/a/1537490/6341466

  • 实在搞不定就把Cookie发送就把得到的Cookie转为字符串赋值到HttpWebRequest.Headers[“Cookie”]中.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值