gpt4 book ai didi

c# - 在 RestSharp 中复制此 PUT 请求

转载 作者:行者123 更新时间:2023-11-30 17:46:15 25 4
gpt4 key购买 nike

下面是请求在网络应用中的样子。

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 捕获

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

最佳答案

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

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

关于c# - 在 RestSharp 中复制此 PUT 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26417427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com