C#从具有参数的帖子中检索数据

问题描述 投票:0回答:1

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

所以,我试图从货船数据提供网站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 Image

但是很奇怪的是,当我尝试使用Burp Suite发送帖子时,数据的输出就像我想要的一样。

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

你们介意帮我吗?

谢谢

c# post httpclient
1个回答
1
投票

邮递员为C#生成的代码-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

© www.soinside.com 2019 - 2024. All rights reserved.