0

I am working on a desktop application which calls some web services via WebClient POST requests. These same web services are used in web application also.

I am facing a strange problem where in my desktop application request was successful and I got response but some of my requests parameters were not saved. But same request is updateing all the parameters we I call them from web application using jquery.

In web application I am calling web service like this

$.post("/MyService/Account/Register",accountModel, function (data) {
});

and I stingify my json object that is accountModel, my request looks like this when console.log

{"Name":"Lorem","Email":"[email protected]","interest":"[\"1\"]","sectors":"[\"1\",\"2\"]","subscribe":false}

Now when I used same request string to post data from my desktop application all the properties like name, email and subscribe were saved but interest and sectors were not saved.

I want to figure it out that why same request object is working via jquery and not in C# webclient.

Here is my code that I used to post data using WebClient

WebClient client = new WebClient();    
string json = string.Format("{{\"Name\":\"{0}\",\"Email\":\"{1}\",\"interest\":\"[\"{2}\"]","sectors":"[\"{3}\",\"{4}\"]","subscribe":{5} }}","Lorem","[email protected]","1","1","2","false");

    client.Headers[HttpRequestHeader.ContentType] = "application/json";

    string result = client.UploadString("http://Server.com/MyService/Account/Register", json);

Please someone help me to resolve this issue that I am not getting any error but some of my parameters were not updates.

I want to clear that I do not have any code or documentation access to web service.

UPDATE

As per Jasen's comment here are requests captured with fiddler

JQuery request

POST http://Server.com/MyService/Account/Register HTTP/1.1
    Host: server.com
    Connection: keep-alive
    Content-Length: 463
    Accept: */*
    Origin: http://server.com
    X-Requested-With: XMLHttpRequest
    User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36
    Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    Referer: http://server.com/MyService/Account/Register/
    Accept-Encoding: gzip, deflate
    Accept-Language: en-US,en;q=0.8
    Cookie: .ASPXAUTH=7A14FC68B72078BAE43A623B94A901180C72093CCE222BBD98EE2AE7E2612078D1E3B7D8860905A4F7B2D75FD67E9274A0A5C40760A5AF703F970504380EBAF8B3D09A15F0B70090ACF4882DC58885F7CF12473BF55647840F3080ADD2C19249

Name=Lorem&[email protected]&interest=%5B%221%22%5D&sectors=%5B%221%22%2C%222%22%5D&subscribe=false

WebClient Request

POST http://server.com/MyService/Account/Register HTTP/1.1
Content-Type: application/json
Host:server.com
Cookie: .ASPXAUTH=F586C63F64186E13EB6EC19AAB25A531A0EDA5B7B601013550ADD629C1481EC3F080DDB5F06D691CB8F81EE8631EF8859F82CF7DD3F2ED2A597AA971A53E80141EDD6EA549784AD7EAE8E144F0CD3196A44316F29C08E0C5383A7231A1B6C5EF
Content-Length: 536
Expect: 100-continue

{"Name":"Lorem","Email":"[email protected]","sectors":["1","2"],"interest":["1"],"subscribe":false}

Shall I send my webclient request as URL encoded string like we can see in jquery request?

4
  • 1
    1) Put a debug stop in and check that json is well-formed. 2) Make the request with a network debugging monitor like Fiddler -- What does the data look like going over the wire and does it match the jQuery request? 3) Instead of serializing the data yourself, which can be error-prone, use JsonConvert.SerializeObject().
    – Jasen
    Oct 26, 2015 at 19:01
  • @Jasen I have added added jquery and webclient requests captured with fiddler. Please suggest if it gives you some hint Oct 27, 2015 at 4:55
  • One difference is the Content-Type header and body format. What do the Fiddler responses look like?
    – Jasen
    Oct 27, 2015 at 17:59
  • @Jasen thanks for you help. I have resolved my issue. Posted my answer below. Oct 27, 2015 at 18:40

1 Answer 1

1

Finally I got the solution with help of fiddler. Thanks to Jasen for suggesting fiddler to see requests

Here is my working code

WebClient client = new WebClient();
string result = client.UploadValues("http://Server.com/MyService/Account/Register", new NameValueCollection()
{
{"Name","Lorem"},
{"Email","[email protected]"},
{"interest","[\"1\"]"},
{"sectors","[\"1\",\"2\"]"},
{"interest","false"}
});

Yes I used UploadValues method instead of UploadData or UploadString. Also note that I have removed the content type json declaration from my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.