gpt4 book ai didi

.net - 如何在 RestSharp 中向请求正文添加文本

转载 作者:数据小太阳 更新时间:2023-10-29 01:36:42 27 4
gpt4 key购买 nike

我正在尝试使用 RestSharp 来使用 Web 服务。到目前为止,一切进展顺利(为 John Sheehan 和所有贡献者干杯!)但我遇到了障碍。假设我想将 XML 以其已经序列化的形式(即,作为字符串)插入到我的 RestRequest 主体中。是否有捷径可寻?看起来 .AddBody() 函数在幕后进行序列化,所以我的字符串被变成了 <String /> .

非常感谢任何帮助!

编辑:请求了我当前代码的示例。见下文——

private T ExecuteRequest<T>(string resource,
RestSharp.Method httpMethod,
IEnumerable<Parameter> parameters = null,
string body = null) where T : new()
{
RestClient client = new RestClient(this.BaseURL);
RestRequest req = new RestRequest(resource, httpMethod);

// Add all parameters (and body, if applicable) to the request
req.AddParameter("api_key", this.APIKey);
if (parameters != null)
{
foreach (Parameter p in parameters) req.AddParameter(p);
}

if (!string.IsNullOrEmpty(body)) req.AddBody(body); // <-- ISSUE HERE

RestResponse<T> resp = client.Execute<T>(req);
return resp.Data;
}

最佳答案

下面是如何将纯 xml 字符串添加到请求正文中:

req.AddParameter("text/xml", body, ParameterType.RequestBody);

关于.net - 如何在 RestSharp 中向请求正文添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5095692/

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