zoukankan      html  css  js  c++  java
  • C# 发送HTTP请求(可加入Cookies)

    C# 发送HTTP请求(可加入Cookies)

     MNTM在2018年11月16日发布

            在C#中,发送一个HTTP请求还是相当容易的,而且与Java的方法特别相似,我们并不需要使用socket来实现这个功能,用C#的HttpWebRequest类就能实现了。最后写出的函数是可以请求一个指定URL的,并且可以带上Cookies。

    0x01 HttpWebRequest类的使用方法

    构造HttpWebRequest对象

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

    Url变量为要访问的Url

    设置属性(设置请求头)

    request.Method = "POST"; //GET或POST请求
    request.ContentType = "application/x-www-form-urlencoded"; //Content-Type头
    request.ContentLength = postDataStr.Length; //设置Content-Length头
    

    常用属性

    Method    请求方式
    ContentType    Content-Type头
    ContentLength    Content-Length头,正文的长度
    Accept    Accept头,接受的类型
    KeepAlive     是否保持HTTP连接
    Headers    请求头的集合,可以添加/修改/删除某个请求头
    UserAgent    User-Agent头,用于设置浏览器名
    Proxy    获取或设置代理服务器的信息

    常用函数方法

    获取请求流(一般用于添加表单数据等)

    request.GetRequestStream();


    获取响应对象

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

    添加请求头

    request.Headers.Add("xxx", val); //添加请求头xxx,的值为val

    0x02 HttpWebResponse类的使用方法

    常用属性

    Headers    请求头的集合,可以添加/修改/删除某个请求头
    ContentEncoding    响应包的编码格式
    ContentLength    Content-Length头,正文的长度
    Cookies    回传的Cookies集合
    StatusCode    HTTP响应状态码

    常用函数方法

    获取响应流(一般用于获取回传数据包的正文内容)

    response.GetResponseStream();
    

    获取响应头

    response.GetResponseHeader("xxx");
    

    0x03 封装一个HttpRequest类

    代码如下

    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.IO;
    using System.Text;
    
    /// <summary>
    ///HttpRequest 的摘要说明
    /// </summary>
    public class HttpRequest{
        public static string Post(string Url, string postDataStr,string cookies){
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            if(cookies!=null)
                request.Headers.Add("Cookie", cookies);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postDataStr.Length;
    
            // 参数
            StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
            writer.Write(postDataStr);
            writer.Close();
    
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string encoding = response.ContentEncoding;
            if (encoding == null || encoding.Length < 1)
            {
                encoding = "UTF-8"; //默认编码  
            }
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
            string retString = reader.ReadToEnd();
            return retString;
        }
    
        public static string Get(string Url,string cookies){
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "GET";
            if(cookies!=null)
                request.Headers.Add("Cookie", cookies);
            request.ContentType = "application/x-www-form-urlencoded";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string encoding = response.ContentEncoding;
            if (encoding == null || encoding.Length < 1)
            {
                encoding = "UTF-8"; //默认编码  
            }
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
            string retString = reader.ReadToEnd();
            return retString;
        }
    
        public HttpRequest()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
    }
  • 相关阅读:
    flock对文件锁定读写操作的问题 简单
    hdu 2899 Strange Fuction(二分)
    hdu 2199 Can you solve this equation? (二分)
    poj 3080 Blue Jeans (KMP)
    poj 2823 Sliding Window (单调队列)
    poj 2001 Shortest Prefixes (trie)
    poj 2503 Babelfish (trie)
    poj 1936 All in All
    hdu 3507 Print Article (DP, Monotone Queue)
    fzu 1894 志愿者选拔 (单调队列)
  • 原文地址:https://www.cnblogs.com/grj001/p/12223236.html
Copyright © 2011-2022 走看看