1

I have a problem post string to form and read. Problem is they get away but need to do so sophisticated and it was very fast. Absolutely perfect multithreaded or asynchronous. Thank you very for your help. This is my code.

private static void AsyncDown()
    {
        const string url = "http://whois.sk-nic.sk/index.jsp";
        const string req = "PREM-0001";
        var client = new HttpClient();

        var pairs = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("text", "PREM-0001")
        };

        FormUrlEncodedContent content = new FormUrlEncodedContent(pairs);

        HttpResponseMessage response = client.PostAsync("http://whois.sk-nic.sk/index.jsp", content).Result;

        if (response.IsSuccessStatusCode)
        {

            HttpContent stream = response.Content;
            Task<string> data = stream.ReadAsStringAsync();
        }
    }
4

2 回答 2

0

也许该站点自上次发布以来已更改,但现在请求参数名称是 whois 而不是文本。如果一年前也是这种情况,那就是它不起作用的原因。

今天网站也响应get,即http://whois.sk-nic.sk/index.jsp?whois=PREM-0001

带get的完整代码:

private async Task<string> Get(string code)
{
    using (var client = new HttpClient())
    {
        var requestUri = String.Format("http://whois.sk-nic.sk/index.jsp?whois={0}", code);

        var data = await client.GetStringAsync(requestUri);

        return data;
    }
}

带帖子的完整代码:

private async Task<string> Post()
{
    using (var client = new HttpClient())
    {
        var postData = new KeyValuePair<string, string>[]
        {
            new KeyValuePair<string, string>("whois", "PREM-0001"),
        };

        var content = new FormUrlEncodedContent(postData);

        var response = await client.PostAsync("http://whois.sk-nic.sk/index.jsp", content);

        if (!response.IsSuccessStatusCode)
        {
            var message = String.Format("Server returned HTTP error {0}: {1}.", (int)response.StatusCode, response.ReasonPhrase);
            throw new InvalidOperationException(message);
        }

        var data = await response.Content.ReadAsStringAsync();

        return data;
    }
}

或者可以使用解析器,因为我猜提取返回的值是最终目标:

private void HtmlAgilityPack(string code)
{
    var requestUri = String.Format("http://whois.sk-nic.sk/index.jsp?whois={0}", code);

    var request = new HtmlWeb();

    var htmlDocument = request.Load(requestUri);

    var name = htmlDocument.DocumentNode.SelectSingleNode("/html/body/table[1]/tr[5]/td/table/tr[2]/td[2]").InnerText.Trim();
    var organizations = htmlDocument.DocumentNode.SelectSingleNode("/html/body/table[1]/tr[5]/td/table/tr[3]/td[2]").InnerText.Trim();
}
于 2014-05-29T22:04:37.267 回答
0

在黑暗中粗略地刺探你的问题是什么,我猜你在阅读你的电话回复时遇到了麻烦。

当内容发布到服务器时,

HttpResponseMessage response 
    = client.PostAsync("http://whois.sk-nic.sk/index.jsp", content).Result;

    if (response.IsSuccessStatusCode)
    {

        HttpContent stream = response.Content;
        Task<string> data = stream.ReadAsStringAsync();
    } 

它是异步执行的,因此即使结果(很可能)还不可用,代码也会继续执行。因此,检查response.IsSuccessStatusCode不会给您预期的行为。

通过添加await关键字将您的调用更改为如下所示:

HttpResponseMessage response 
    = await client.PostAsync("http://whois.sk-nic.sk/index.jsp", content);

然后,将流的读取更改为也使用 await:

    if (response.IsSuccessStatusCode)
    {
        var data = await response.Content.ReadAsStringAsync();
    }

编辑:混淆了一些等待对象并更正了代码清单。

编辑 2:这是我用来从给定 URL 成功下载 HTML 页面的完整 LINQPad 脚本。

var client = new HttpClient();

var pairs = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>("text", "PREM-0001")
};

FormUrlEncodedContent content = new FormUrlEncodedContent(pairs);
HttpResponseMessage response = await client.PostAsync("http://whois.sk-nic.sk/index.jsp", content);

if (response.IsSuccessStatusCode)
{
    var data = await response.Content.ReadAsStringAsync();
    //data.Dump(); //uncomment previous if using LINQPad
}
于 2013-04-01T15:26:50.313 回答