本文介绍了排除了HTTPClient异步POST和读取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题后字符串形成和读取。问题是他们离开,但需要做的非常成熟,这是非常快的。绝对完美的多线程或异步的。非常感谢您的帮助。
这是我的code。

 私有静态无效AsyncDown()
    {
        常量字符串URL =htt​​p://whois.sk-nic.sk/index.jsp;
        常量字符串REQ =preM-0001;
        VAR的客户=新的HttpClient();        对VAR =新名单< KeyValuePair<字符串,字符串>>
        {
            新KeyValuePair<字符串,字符串>(文字,preM-0001」)
        };        FormUrlEn codedContent内容=新FormUrlEn codedContent(对);        HTT presponseMessage响应= client.PostAsync(http://whois.sk-nic.sk/index.jsp,内容)。结果;        如果(response.IsSuccessStatus code)
        {            HttpContent流= response.Content;
            任务<串GT;数据= stream.ReadAsStringAsync();
        }
    }


解决方案

也许自去年后该网站已经改变,但现在请求参数名的whois不是文本。如果这在一年前是太大的情况下,这就是为什么它没有工作。

今天现场回应太,即

全部code用GET:

 专用异步任务<串GT;获取(字符串code)
{
    使用(VAR的客户=新的HttpClient())
    {
        VAR requestUri =的String.Format(http://whois.sk-nic.sk/index.jsp?whois={0},code);        VAR数据=等待client.GetStringAsync(requestUri);        返回的数据;
    }
}

全部code。与岗位:

 专用异步任务<串GT;邮政()
{
    使用(VAR的客户=新的HttpClient())
    {
        VAR POSTDATA =新KeyValuePair<字符串,字符串> []
        {
            新KeyValuePair<字符串,字符串>(域名注册,preM-0001),
        };        VAR内容=新FormUrlEn codedContent(POSTDATA);        VAR响应=等待client.PostAsync(http://whois.sk-nic.sk/index.jsp内容);        如果(!response.IsSuccessStatus code)
        {
            VAR消息=的String.Format(服务器返回的HTTP错误{0}:{1}。(INT)response.Status code,response.ReasonPhrase);
            抛出新的InvalidOperationException异常(消息);
        }        VAR数据=等待response.Content.ReadAsStringAsync();        返回的数据;
    }
}

还是因为我猜提取返回值的解析器可以用来为最终目标:

 私人无效HtmlAgilityPack(字符串code)
{
    VAR requestUri =的String.Format(http://whois.sk-nic.sk/index.jsp?whois={0},code);    VAR要求=新HtmlWeb();    VAR的HTMLDocument = request.Load(requestUri);    变量名称= htmlDocument.DocumentNode.SelectSingleNode(\"/html/body/table[1]/tr[5]/td/table/tr[2]/td[2]\").InnerText.Trim();
    VAR组织= htmlDocument.DocumentNode.SelectSingleNode(\"/html/body/table[1]/tr[5]/td/table/tr[3]/td[2]\").InnerText.Trim();
}

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();
        }
    }
解决方案

Maybe the site has changed since last post but now the request parameter name is whois not text. If this was the case a year ago too that's why it didn't work.

Today site responds to get too, i.e. http://whois.sk-nic.sk/index.jsp?whois=PREM-0001

Full code with 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;
    }
}

Full code with post:

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;
    }
}

Or a parser could be used because I guess extracting the returned values is the final goal:

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();
}

这篇关于排除了HTTPClient异步POST和读取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:09