Reputation: 1
I am facing this strange behavioure inside my asp.net mvc-4 web application. now i have the following WebClient()
to upload a string to an external 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));
}
and here is the json object i am sending (before url encoding it as per the API documentation which require to url encode the json):-
INPUT_DATA="{\"operation\":{\"Details\":{\"RESOURCENAME\":\"test server 500123\",\"ACCOUNTNAME\":\"1\",\"RESOURCETYPE\":\"Windows\",\"PASSWORD\":\"<1234>\",\"NOTES\":null,\"Account Type\":null,\"RESOURCEURL\":null,\"OWNERNAME\":\"admin\",\"RESOURCEGROUPNAME\":\"other CUstomers\",\"DNSName\":\"-\",\"DEPARTMENT\":null,\"LOCATION\":null,\"RESOURCECUSTOMFIELD\":[{\"CUSTOMLABEL\":\"Asset Tag\",\"CUSTOMVALUE\":\"H1007228\"}],\"ACCOUNTCUSTOMFIELD\":[{\"CUSTOMLABEL\":\"Account Type\",\"CUSTOMVALUE\":\"domain\"}]}},\"createAccount\":{\"operation\":{\"Details\":{\"ACCOUNTLIST\":[]}}}}"
Now if my json object contain any value which have the following characters <
or >
the web client's UploadString method will get the following error:-
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)
On the other hand, If i copy/paste the above json string inside the Firefox RESTClient debugger, and i run the call inside the RESTClient tool then operation will work fine...
so can anyone adivce why if i call the web service from my asp.net WebClient
and the json object contain <
or >
i will get the following error The remote server returned an error: (599)
? while calling the web service using the same json string using FireFox's RESTClient debugger will work fine ?
Final note ,, If my json string does not contain any of these 2 characters <
or >
everything will be working fine.
edit
now when i use the RESTCleint tool to make a Post request to the 3rd party API ,, i can see the following response from the 3rd party API.. i got the following response header :-
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-Frame-Options: SAMEORIGIN
Upvotes: 0
Views: 4437
Reputation: 5141
HTTP Status 500-599
means that the problem is within the server itself and not with the request.
If you can see the request headers of the one generated by FireFox's RESTClient
then you can compare it with the one generated by WebClient
just to help you isolate the issue.
If all else is still the same then you just found a bug on the web service.
Upvotes: 1