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 テキストではありません。これが 1 年前にも当てはまる場合、それが機能しなかった理由です。

今日のサイトも 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

あなたの問題が何であるかを暗闇の中で大まかに突き刺すと、電話の応答を読むのに苦労していると思います。

コンテンツがサーバーに POST されると、

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

編集: いくつかの await オブジェクトが混同され、コード リストが修正されました。

編集 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 に答える