在RestSharp中复制此PUT请求

时间:2014-10-17 03:30:14

标签: c# rest restsharp

以下是Web应用程序中的请求。

Request URL:http://myurl.com/rest
Request Method:PUT
Status Code:200 Ok
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:60
Content-Type:application/json
Cookie:ui_dom_other=block; session=sessionkey; acct_table#pageNavPos=1; ui_usr_feat=block
Host:http://myurl
Origin:http://myurl.com
Referer:http://myurl.com/referer
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
Request Payload view parsed
{"table":"users","settings":[{"name":"dnd","value":"true"}]}
Response Headersview source
Cache-Control:no-cache
Content-Length:2
Content-Type:application/json

数据位于请求有效负载字段

{"table":"users","settings":[{"name":"dnd","value":"true"}]}

以下是我当前的C#RestSharp代码

            // Initiate Rest Client
            var client = new RestClient("http://myurl.com");
            var request = new RestRequest("resturl/restrequest");
            // Set headers, method and cookies
            request.Method = Method.PUT;
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "*/*");
            request.AddCookie("session", sessionKey);
            // Set Data format
            request.RequestFormat = DataFormat.Json;
            // Set Data
            string theString = "{'table':'users','settings':[{'name':'dnd','value':'true'}]}";
            request.AddBody(theString);
            // Execute
            var test123 = client.Execute(request);

我已经能够成功完成所有GET / POST呼叫,但是PUT没有成功。

Fiddler Capture

网络应用 - 工作

PUT http://myurl HTTP/1.1
Host: http://myurl
Connection: keep-alive
Content-Length: 60
Origin: hhttp://myurl
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
Content-Type: application/json
Accept: */*
Referer: http://myurl
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: ui_dom_other=block; session=sessionkey; acct_table#pageNavPos=1; ui_usr_feat=block

{"table":"users","settings":[{"name":"dnd","value":"true"}]}

C#应用程序 - 不工作

PUT http://myurl HTTP/1.1
Accept: */*
User-Agent: RestSharp/105.0.1.0
Content-Type: text/xml
Host: http://myurl
Cookie: session=sessionkey
Content-Length: 10
Accept-Encoding: gzip, deflate

<String />

1 个答案:

答案 0 :(得分:1)

考虑添加对象,而不是添加字符串作为有效负载:

request.AddBody(new { table = "users", settings = new[] { new { name = "dnd", value = "true" } } });