我正在尝试使用 RestSharp 从 POST 请求返回字符串响应。
这就是我正在尝试的
public IRestResponse PostNewLocation(string Name, string Type, Nullable<Guid> ParentId, string Location)
{
object tmp = new
{
name = Name,
type = Type,
parentId = ParentId,
Location = Location
};
string json = JsonConvert.SerializeObject(tmp);
var Client = new RestClient();
Client.BaseUrl = new Uri(BaseURL);
var request = new RestRequest(Method.POST);
request.Resource = string.Format("/Sample/URL?");
request.AddParameter("application/json", json, ParameterType.RequestBody);
IRestResponse response = Client.Execute(request);
Console.Write(response.Content);
if (!IsReturnedStatusCodeOK(response))
{
throw new HttpRequestException("Request issue -> HTTP code:" + response.StatusCode);
}
return response.Content;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
错误 CS0029 无法将类型“字符串”隐式转换为“RestSharp.IRestResponse”
在这条线上
return response.Content;
Run Code Online (Sandbox Code Playgroud)
如何string
从 RestSharp返回响应?
响应是一个 GUID 字符串。
如果要将响应的原始内容作为字符串返回,请更改PostNewLocation
to的返回类型string
:
public string PostNewLocation (
...
return response.Content;
}
Run Code Online (Sandbox Code Playgroud)
或者 returnresponse
而不是response.Content
如果你想返回的实例IRestResponse
(你以后可以得到一个原始内容):
public IRestResponse PostNewLocation (
...
return response;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6971 次 |
最近记录: |