簡體   English   中英

錯誤捕獲來自(第三方)Web服務的響應信息

[英]Capture response information from (3rd party) web service on error

我有以下代碼,該代碼將XML提交給第三方Web服務,該錯誤在“ req.GetResponse()”上錯誤地(此刻是有意的)錯誤,如下所述。

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

來自GetResponse()的錯誤

引發異常:System.dll中的'System.New.WebExtension'其他信息:遠程服務器返回錯誤:(400)Bad Request

當我在Fiddler中跟蹤此調用時,我可以看到該服務的響應還包含一個更有用的錯誤(如下所述;原始視圖),我試圖將其弄到:

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'

在變量上有一個“監視”似乎並沒有顯示我可以從哪里獲得(可能是一些我忽略了的簡單事物)

最終找到了解決方案; “結果”包含過程中的內容; 還有更多工作要做,但這是我所需要的基礎。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM