2011-05-10 30 views
6

我有一段代碼,即作品使用的HttpWebRequestHttpWebResponse但我想將其轉換爲使用的HttpClientHttpResponseMessage的HttpWebRequest Vs的HttpClient的

這是該工程的代碼塊...

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(serviceReq); 

request.Method = "POST"; 
request.ContentType = "text/xml"; 
string xml = @"<?xml version=""1.0""?><root><login><user>flibble</user>" + 
    @"<pwd></pwd></login></root>"; 
request.ContentLength = xml.Length; 
using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream())) 
{ 
    dataStream.Write(xml); 
    dataStream.Close(); 
} 

HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

這就是我想和來取代它,如果我能得到它的工作的代碼。

/// <summary> 
/// Validate the user credentials held as member variables 
/// </summary> 
/// <returns>True if the user credentials are valid, else false</returns> 
public bool ValidateUser() 
{ 
    bool valid = false; 

    try 
    { 
     // Create the XML to be passed as the request 
     XElement root = BuildRequestXML("LOGON"); 

     // Add the action to the service address 
     Uri serviceReq = new Uri(m_ServiceAddress + "?obj=LOGON"); 


     // Create the client for the request to be sent from 
     using (HttpClient client = new HttpClient()) 
     { 
      // Initalise a response object 
      HttpResponseMessage response = null; 

      // Create a content object for the request 
      HttpContent content = HttpContentExtensions. 
       CreateDataContract<XElement>(root); 

      // Make the request and retrieve the response 
      response = client.Post(serviceReq, content); 

      // Throw an exception if the response is not a 200 level response 
      response.EnsureStatusIsSuccessful(); 

      // Retrieve the content of the response for processing 
      response.Content.LoadIntoBuffer(); 

      // TODO: parse the response string for the required data 
      XElement retElement = response.Content.ReadAsXElement(); 
     } 
    } 
    catch (Exception ex) 
    { 
     Log.WriteLine(Category.Serious, 
      "Unable to validate the Credentials", ex); 
     valid = false; 
     m_uid = string.Empty; 
    } 

    return valid; 
} 

我認爲這個問題是創建內容對象和XML沒有被正確連接(也許)。

+0

究竟什麼不行?不編譯?或者你有運行時錯誤?哪些錯誤? – fretje 2011-05-10 15:35:45

+0

什麼是錯誤? – Aliostad 2011-05-10 15:36:07

+0

發送請求,但處理入站請求的服務認爲沒有數據,因此會返回「未經授權的訪問」,這是該服務的默認響應。 – TeamWild 2011-05-10 15:38:39

回答

1

我很想知道原因,爲什麼一個方法是行不通的,而另一個不,但我只是不沒有時間再挖掘。 {:O(

無論如何,這裏是我的發現

使用以下

HttpContent content = HttpContentExtensions.Create(root, Encoding.UTF8, "text/xml"); 

創建請求的內容時,會發生故障但是當你創建喜歡的內容是否能夠正常工作。這...

HttpContent content = HttpContent.Create(root.ToString(), Encoding.UTF8, "text/xml"); 

最後的工作職能是:

/// <summary> 
/// Validate the user credentials held as member variables 
/// </summary> 
/// <returns>True if the user credentials are valid, else false</returns> 
public bool ValidateUser() 
{ 
    bool valid = false; 

    try 
    { 
     // Create the XML to be passed as the request 
     XElement root = BuildRequestXML("LOGON"); 

     // Add the action to the service address 
     Uri serviceReq = new Uri(m_ServiceAddress + "?obj=LOGON"); 

     // Create the client for the request to be sent from 
     using (HttpClient client = new HttpClient()) 
     { 
      // Initalise a response object 
      HttpResponseMessage response = null; 

      #if DEBUG 
      // Force the request to use fiddler 
      client.TransportSettings.Proxy = new WebProxy("127.0.0.1", 8888); 
      #endif 

      // Create a content object for the request 
      HttpContent content = HttpContent.Create(root.ToString(), Encoding.UTF8, "text/xml"); 

      // Make the request and retrieve the response 
      response = client.Post(serviceReq, content); 

      // Throw an exception if the response is not a 200 level response 
      response.EnsureStatusIsSuccessful(); 

      // Retrieve the content of the response for processing 
      response.Content.LoadIntoBuffer(); 

      // TODO: parse the response string for the required data 
      XElement retElement = response.Content.ReadAsXElement(); 
     } 
    } 
    catch (Exception ex) 
    { 
     Log.WriteLine(Category.Serious, "Unable to validate the user credentials", ex); 
     valid = false; 
     m_uid = string.Empty; 
    } 

    return valid; 
} 

謝謝。

1

HttpClient.Post方法有一個重載接受一個contentType參數,試試這個:

// Make the request and retrieve the response 
response = client.Post(serviceReq, "text/xml", content); 
+0

謝謝。正如你在下面看到的,我正處於同樣的思考過程。我所看到的問題似乎與創建內容對象的方式有關,而不是所使用的post方法。 – TeamWild 2011-05-12 07:33:46