RestSharp获取请求的完整URL

时间:2017-01-01 14:55:04

标签: c# asp.net .net restsharp

有没有办法获取RestSharp请求的完整URL,包括其资源和查询字符串参数?

I.E对此请求:

RestClient client = new RestClient("http://www.some_domain.com");
RestRequest request = new RestRequest("some/resource", Method.GET);
request.AddParameter("some_param_name", "some_param_value", ParameterType.QueryString);

IRestResponse<ResponseData> response = client.Execute<ResponseData>(request);

我想获得完整的请求网址:

http://www.some_domain.com/some/resource?some_param_name=some_param_value

2 个答案:

答案 0 :(得分:40)

要使用RestClient.BuildUri()

获取完整网址

具体而言,在此示例中使用client.BuildUri(request)

RestClient client = new RestClient("http://www.some_domain.com");
RestRequest request = new RestRequest("some/resource", Method.GET);
request.AddParameter("some_param_name", "some_param_value", ParameterType.QueryString);

IRestResponse<ResponseData> response = client.Execute<ResponseData>(request);

var fullUrl = client.BuildUri(request);

答案 1 :(得分:0)

嗯,这很棘手。 要获取完整的请求URL,请使用RestClient.Execute(request).ResponseUri以确保它是已发送的请求。

在此示例中:

RestClient client = new RestClient("http://www.some_domain.com");
RestRequest request = new RestRequest("some/resource", Method.GET);
request.AddParameter("some_param_name", "some_param_value", ParameterType.QueryString);

IRestResponse<ResponseData> response = client.Execute<ResponseData>(request);
Uri fullUrl = response.ResponseUri;

此代码:

Console.WriteLine(string.Format("response URI: {0}", response.ResponseUri.ToString()));

返回:

response URI: http://www.some_domain.com/some/resource?some_param_name=some_param_value