在我的asp.net mvc-4 web应用程序中,我面临着这种奇怪的行为。现在,我有了以下WebClient()
来将字符串上载到外部API:
var data = JsonConvert.SerializeObject(mainresourceinfo);
using (WebClient wc = new WebClient())
{
string url = currentURL + "resources?AUTHTOKEN=" + pmtoken;
Uri uri = new Uri(url);
wc.Encoding = System.Text.Encoding.UTF8;
wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
crudoutput = wc.UploadString(uri, "INPUT_DATA=" + HttpUtility.UrlEncode(data));
}
下面是我要发送的json对象(在url按照需要对json编码的API文档编码它之前):
INPUT_DATA="{\"operation\":{\"Details\":{\"RESOURCENAME\":\"test服务器500123\",\“ACCOUNTNAME\”:\“1”,\“RESOURCETYPE\”:\“<1234>\\”,\"NOTES\":null,\"Account Type\":null,\"RESOURCEURL\":null,\"OWNERNAME\":\"admin\",\“RESOURCEGROUPNAME\”:\“其他客户”,\"DNSName\":\"-\",\"DEPARTMENT\":null,\"LOCATION\":null,\"RESOURCECUSTOMFIELD\":{\"CUSTOMLABEL\":\"Asset标记\,\“CUSTOMVALUE\”:\“H1007228},\"ACCOUNTCUSTOMFIELD\":{\"CUSTOMLABEL\":\"Account类型\”,\“CUSTOMVALUE\”:\“域\”},\"createAccount\":{\"operation\":{\"Details\":{\"ACCOUNTLIST\":[]}}}}“
现在,如果我的json对象包含具有以下字符<
或>
的值,则web客户端的UploadString方法将得到以下错误:
System.Net.WebException occurred
HResult=-2146233079
Message=The remote server returned an error: (599).
Source=System
StackTrace:
at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request)
at System.Net.WebClient.UploadString(Uri address, String method, String data)
at System.Net.WebClient.UploadString(Uri address, String data)
另一方面,如果我在火狐RESTClient调试器中复制/粘贴上面的json字符串,并且在RESTClient工具中运行调用,那么操作将很好.
因此,如果我从asp.net WebClient
调用web服务,而json对象包含<
或>
,为什么会得到以下错误The remote server returned an error: (599)
呢?使用同一个json字符串调用web服务时,使用火狐的RESTClient调试器可以正常工作吗?
最后请注意,如果我的json字符串不包含这两个字符中的任何一个( <
或>
),那么一切都将正常工作。
编辑
现在,当我使用RESTCleint工具向第三方API发出Post请求时,我可以看到来自第三方API的以下响应。我得到以下响应标题:-
Status Code: 200 OK
Cache-Control: no-store
Content-Type: text/html;charset=UTF-8
Date: Thu, 04 Aug 2016 15:29:37 GMT
Pragma: no-cache
Server: PMP
Set-Cookie: JSESSIONID=B9CA7B68CD5706C3B82FE925B3AB3244; Path=/; Secure; HttpOnly
Strict-Transport-Security: max-age=7776000; includeSubdomains
Transfer-Encoding: chunked
X-帧-选项: SAMEORIGIN
发布于 2016-08-04 02:15:54
HTTP Status 500-599
意味着问题发生在服务器本身,而不是请求中。
如果您可以看到FireFox's RESTClient
生成的请求头,那么您可以将其与WebClient
生成的请求头进行比较,以帮助您隔离问题。
如果所有这些都是相同的,那么您刚刚在web服务上发现了一个bug。
https://stackoverflow.com/questions/38757270
复制相似问题