简体   繁体   中英

POST JSON to webservice using WebClient C#

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":"abc@abc.com","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","abc@abc.com","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=abc@abc.com&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":"abc@abc.com","sectors":["1","2"],"interest":["1"],"subscribe":false}

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

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","abc@abc.com"},
{"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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM