2013-04-01 25 views
1

我有一個問題的字符串來形成和閱讀。問題是他們逃脫了,但需要做得如此精巧,而且速度非常快。絕對完美的多線程或異步。非常感謝您的幫助。 這是我的代碼。解決HTTPClient異步POST和讀取結果

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

這很難理解你在問什麼。您可以嘗試用您的母語編寫問題,並使用Google翻譯將其轉換爲英文。 –

+1

@nørdic我不確定實際的問題是什麼。你能爲我們澄清嗎?你有沒有試過小提琴手? – Justin

+0

您是否在詢問如何閱讀這些請求的回覆?您遇到的問題是您不確定如何處理異步部分?您發佈的代碼似乎表明 –

回答

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將不會給你你期望的行爲。

更改您的電話加入到這個樣子的等待關鍵字:

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

然後,更改數據流的讀取使用等待,以及:

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 
} 
+0

謝謝。但是,如果我嘗試添加await,這是HttpResponseMessage的錯誤消息。錯誤「無法等待'System.Net.Http.HttpResponseMessage」 – msavara

+0

是的,我忘了它是你正在等待的內容的閱讀 - 看到更新的代碼。 –

+0

它不這樣工作。不要來這部分。 「await response.Content.ReadAsStringAsync();」 – msavara

0

也許該網站自去年後改變,但現在的請求參數的名稱是域名註冊沒有文字。如果這是一年前的情況,那就是爲什麼它不起作用。

今天現場迴應太,即http://whois.sk-nic.sk/index.jsp?whois=PREM-0001

以獲取完整代碼:

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