C#爬虫入门之简单POST请求-有道翻译

简要说明

HTTP请求头(Headers)

user-agent:
伪装浏览器,应对网站反爬虫措施
Referer:
告诉服务器该网页是从哪个页面链接过来的
Content-Type:
告诉网站发送请求的媒体类型

POST请求字符串格式:

"{0}={1}&{2}={3}&{4}={5}..."
eg:"query=他改变了&i=1926&t=蛤"

构建请求字符串

/// 
/// 把字典转化为请求字符串
/// 
/// 参数字典
/// 返回请求字符串
public static string GetPostArgs(Dictionary dict)
{
    StringBuilder sb = new StringBuilder();
    bool first = true;
    foreach (var item in dict)
    {
        if(first)
        {
            sb.Append(item.Key);
            sb.Append("=");
            sb.Append(item.Value);
            first = false;
        }
        else
        {
            sb.Append("&");
            sb.Append(item.Key);
            sb.Append("=");
            sb.Append(item.Value);
        }
    }
    return sb.ToString();
}

创建POST请求发送表单并接受返回信息

/// 
/// 发送Post请求
/// 
/// 连接
/// HTTP头
/// 请求字符串
/// 返回结果
public static string PostInf(string url, WebHeaderCollection headers, string str)
{
    //创建HTTP请求
    var re = WebRequest.Create(url) as HttpWebRequest;
    //设置请求头
    re.Headers = headers;
    //设置访问类型为POST
    re.Method = "POST";
    //写入请求信息
    using(StreamWriter sw=new StreamWriter(re.GetRequestStream()))
    {
        sw.WriteLine(str);
    }
    //获取相应内容
    var ans = re.GetResponse();
    using (var st = new StreamReader(ans.GetResponseStream()))
    {
        return st.ReadToEnd();
    }
}

Main方法

var url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule";
var headers = new WebHeaderCollection();
headers["Content-Type"] ="application/x-www-form-urlencoded";
headers["user-agent"]="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36";
headers["Referer"]="https://www.lagou.com/jobs/list_unity3d?labelWords=&fromSearch=true&suginput=";
var dict = new Dictionary()
{
    {"i",ReadLine() },
    {"from","AUTO" },
    {"to","AUTO" },
    {"smartresult","dict" },
    {"client", "fanyideskweb" },
    {"salt","15808837717114" },
    {"sign","22a6ee9a07d4821f04e50bd029f73de0" },
    {"ts" ,"1580883771711" },
    {"bv","38c3ccbde2d50a86f0f9606d2be5a3d8"},
    {"doctype","json" },
    {"version","2.1"},
    {"keyfrom","fanyi.web"},
    {"action","FY_BY_REALTlME" }
};
var str=PostInf(url,headers, GetPostArgs(dict));

处理JSON字符串

注:本次使用DNC自带的json解析工具

using (var dt = JsonDocument.Parse(str))
{
   WriteLine(dt.RootElement.GetProperty("translateResult")[0][0].GetProperty("tgt"));
}

你可能感兴趣的:(http,c#,post)