正在RestSharp中复制此PUT请求

本文关键字:PUT 请求 复制 RestSharp 正在 | 更新日期: 2023-09-27 18:27:00

以下是请求在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并没有成功。

小提琴手捕获

Web应用程序-工作

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 />

正在RestSharp中复制此PUT请求

与其添加字符串作为有效负载,不如考虑添加一个对象:

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