gpt4 book ai didi

c# - python 到 c# (关于服务器请求)

转载 作者:行者123 更新时间:2023-12-01 07:04:23 25 4
gpt4 key购买 nike

我需要将这个 python 代码翻译成 C#:

messages = { "to" :"PhoneNumber" }
body = {
"type" : "SMS",
"contentType" : "COMM",
"from" : "PhoneNumber2",
"subject" :"subject",
"content" : "Hell world",
"messages" : [messages]
}
body2 = json.dumps(body)
headers = {
'Content-Type': 'application/json; charset=utf-8',
'X-ncp-apigw-timestamp': timestamp,
'x-ncp-iam-access-key': access_key,
'x-ncp-apigw-signature-v2': make_signature(uri, access_key)
}

res = requests.post(apiUrl, headers=headers, data=body2)

res.request
res.status_code
res.raise_for_status()

print(res.json())

所以我尝试过:

    public class themessage
{
public string to;
}
public class body
{
public string type;
public string contentType;
public string from;
public string subject;
public string content;
public themessage messages;
}


var obj = new body
{
type = "SMS",
contentType = "COMM",
from = "PN",
subject = "subject",
content = "Hell World",
messages = new themessage
{
to = "PN"
}
};


var client = new RestClient(apiUrl);
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.AddHeader("X-ncp-apigw-timestamp", timestamp);
request.AddHeader("x-ncp-iam-access-key", accessKey);
request.AddHeader("x-ncp-apigw-signature-v2", test);
request.AddJsonBody(obj); // **this is where I'm concerning part**
IRestResponse response = client.Execute(request);

但是,正如您所期望的,无法发布错误消息“未请求格式”的内容。

我在制作JsonBody时出错了吗?或者发帖流程?

感谢您提前提供的所有答复!

最佳答案

继我对 IRestRequest interface 的最后评论之后,您的类层次结构应该类似于

public class themessage
{
public string to;
}

public class body
{
public string type;
public string contentType;
public string from;
public string subject;
public string content;
public themessage[] messages; // <- Array here
}

你用它创建的对象将类似于

var obj = new body
{
type = "SMS",
contentType = "COMM",
from = "PN",
subject = "subject",
content = "Hell World",
messages = new themessage[] { new themessage{to = "PN"} }
};

RestClientRestRquest 的代码保持原样。

关于c# - python 到 c# (关于服务器请求),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58502297/

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