我有以下代码,它将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.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'
观看&#39;关于变量似乎并没有表明我可能从中得到这个(并且很可能是我忽略的简单)
答案 0 :(得分:0)
最终找到了解决方案;其中'result'包含流程中的内容;还有更多工作要做,但这是我需要的基础知识。
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;
}