2012-02-25 97 views
0

我需要使用以下文本/普通主體讀取HTTP響應200 OK。 (任一)如何閱讀HTTP響應正文?

OK 

NUMBER NOT IN LIST 

ERROR 

但我只知道如何讀取HTTP響應,而不是響應正文。以實例說明,將不勝感激

+3

如果你正在使用*這個,那麼它可能與asp.net無關。坦率地說,這不僅僅是WebClient.DownloadString? – 2012-02-25 13:09:26

+0

謝謝。這就是我需要的。 – 2012-02-25 13:21:42

回答

3

可以使用WebClient.DownloadString Method發出HTTP GET請求,並得到HTTP響應正文返回的字符串:

using (var client = new WebClient()) 
{ 
    string result = client.DownloadString("http://example.com/path/to/file"); 

    switch (result) 
    { 
     case "OK": 
     case "NUMBER NOT IN LIST": 
     case "ERROR": 
      break; 
    } 
} 
+0

謝謝。我會試一試。 – 2012-02-25 13:16:15

0
 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://example.com/path/to/file"); 

     request.UseDefaultCredentials = true; 

     if (request.Proxy == null) 
     { 
      request.Proxy = new WebProxy("http://example.com"); 
     } 
     request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; 

HttpWebResponse響應=(HttpWebResponse)(要求.GetResponse());