2016-06-16 78 views
0

我有以下代碼,它將XML提交給第三方Web服務,其中的「req.GetResponse()」錯誤(故意在此刻)錯誤,詳細如下。從的GetResponse從(第三方)Web服務捕獲錯誤的響應信息

byte[] bytes = Encoding.ASCII.GetBytes(myXMLData); 
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(myWebsite); 
using (Stream os = req.GetRequestStream()) 
{ 
    os.Write(bytes, 0, bytes.Length); 
} 
WebResponse response = req.GetResponse(); 
string responseStream = StringFromResponseStream(response); 

錯誤()

拋出異常: 'System.New.WebExtension' 在System.dll中 其他信息:遠程服務器返回一個錯誤:(400)錯誤的請求

當我在Fiddler中跟蹤這個調用時,我可以看到來自服務的響應還包含了一個更有用的錯誤(下面的RAW視圖),我正在嘗試訪問:

HTTP/1.1 400 Bad Request 
Date: Thu, 16 Jun 2016 10:42:26 GMT 
Cache-Control: private 
X-Content-Type-Options: nosniff 
X-Frame-Options: SAMEORIGIN 
Content-Type: text/plain; charset=UTF-8 
Content-Length: 54 
Caseprovider-Credentials: <snip> 
Caseprovider-Credentialshash: <snip> 
Caseprovider-Apiversion: 15 
Connection: close 

No supported action 'SomeName' available for 'SomeValue' 

擁有的變量「手錶」似乎並沒有表現出在那裏可以得到這個從(而且很有可能簡單的東西被我忽略)

回答

0

有最終找到了解決的; 「結果」包含進程中的內容;還有更多事情要做,但這是我所需要的基礎知識。

using (var httpClient = new HttpClient()) 
{ 
    httpClient.BaseAddress = new Uri(myWebSite); 
    httpClient.DefaultRequestHeaders.Accept.Clear(); 
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml")); 

    StringContent stringContent = new StringContent(myXMLData); 
    stringContent.Headers.ContentType = new MediaTypeHeaderValue("text/xml"); 
    HttpResponseMessage httpResponseMessage = httpClient.PostAsync(httpClient.BaseAddress, stringContent).Result; 

    string result = httpResponseMessage.Content.ReadAsStringAsync().Result; 
}