【问题标题】:C# Retrieve data from a Post with parametersC# 使用参数从 Post 中检索数据
【发布时间】:2020-02-09 08:05:46
【问题描述】:

我知道我的问题看起来像是重复的问题,但我找不到对我的问题有用的解决方案。

所以我正在尝试从货船数据提供网站Link(这是一个韩国网站。右侧的黑色按钮是搜索按钮)抓取数据

但为了从中获取数据,必须设置一些单选按钮然后点击搜索。

我以为我可以通过 FormUrlEncodedContent 传递参数值,然后简单地使用 PostAsync,但不知何故我无法让它们通过。

这是我目前的代码

       using (var client = new HttpClient())
       {
            client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
            client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");

            var doc = new HtmlAgilityPack.HtmlDocument();
            var content = new FormUrlEncodedContent(structInfo.ScriptValues);
            var response = await client.PostAsync(structInfo.PageURL, content);
            var responseString = await response.Content.ReadAsStringAsync();
            Console.WriteLine(responseString);
           
       }

        using (WebClient client = new WebClient())
        {
            var reqparm = new System.Collections.Specialized.NameValueCollection();
            reqparm.Add("v_time", "month");
            reqparm.Add("ROCD", "ALL");
            reqparm.Add("ORDER", "item2");
            reqparm.Add("v_gu", "S");
            byte[] responsebytes = client.UploadValues("http://info.bptc.co.kr:9084/content/sw/frame/berth_status_text_frame_sw_kr.jsp", "POST", reqparm);
            string responsebody = Encoding.UTF8.GetString(responsebytes);

            Console.WriteLine(responsebody);
        }

我放在 StructInfo 类中的值

            PageURL = "http://info.bptc.co.kr:9084/content/sw/frame/berth_status_text_frame_sw_kr.jsp",
            ScriptValues = new Dictionary<string, string>
            {
                {"v_time", "month"},
                {"ROCD", "ALL"},
                {"ORDER", "item2"},
                {"v_gu", "S"}
            },

到目前为止,我尝试过的是 HttpClient、WebClient、WebBrowser,但我没有运气。

但奇怪的是,当我尝试使用 Burp Suite 发送帖子时,数据以我想要的方式输出。

过去 4 小时我一直在寻找解决方案,但没有任何运气。

你们愿意帮帮我吗?

谢谢

【问题讨论】:

    标签: c# post httpclient


    【解决方案1】:

    为 C# 生成的代码 - Postman 的 RestSharp

        var client = new RestClient("http://info.bptc.co.kr:9084/Berth_status_text_servlet_sw_kr");
        client.Timeout = -1;
        var request = new RestRequest(Method.POST);
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
        request.AddParameter("v_time", "3days");
        request.AddParameter("ROCD", "ALL");
        request.AddParameter("ORDER", "item2");
        request.AddParameter("v_gu", "S");
        IRestResponse response = client.Execute(request);
        Console.WriteLine(response.Content);
    

    HttpClient 版本

        using var client = new HttpClient();
        var content = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("v_time", "3days"),
            new KeyValuePair<string, string>("ROCD", "ALL"),
            new KeyValuePair<string, string>("ORDER", "item2"),
            new KeyValuePair<string, string>("v_gu", "S"),
        });
        string url = "http://info.bptc.co.kr:9084/Berth_status_text_servlet_sw_kr";
        var response = await client.PostAsync(url, content);
        var bytes = await response.Content.ReadAsByteArrayAsync();
        string responseString = Encoding.UTF8.GetString(bytes);
        Console.WriteLine(responseString);
    

    问题

    如果我们谈论 HttpClient 版本,假设您使用的是 .net core。

    调用 ReadAsStringAsync 时引发异常。 更具体地说,如下: https://github.com/microsoft/referencesource/blob/aaca53b025f41ab638466b1efe569df314f689ea/System/net/System/Net/Http/HttpContent.cs#L95

    响应有 ContentType: text/html; charset=euc-kr.

    问题是 .net 核心不支持开箱即用的韩语字符集。 我的解决方法是改用 ReadAsByteArrayAsync,然后再使用支持的 UTF8 编码器。它虽然拧了韩文字符。 更好的方法是引用 System.Text.Encoding.CodePages 包,然后使用 Encoding.RegisterProvider。 像这样Encoding.GetEncoding can't work in UWP app

    【讨论】:

    • 工作就像一个魅力,但你介意解释为什么我的代码不起作用,但你的?
    • 我添加了问题部分就是答案。长话短说:net core 不支持开箱即用的韩语字符集。
    猜你喜欢
    • 2021-09-02
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    相关资源
    最近更新 更多