RestSharp get full URL of a request

家住魔仙堡 提交于 2019-12-21 06:55:48

问题


Is there a way to get the full url of a RestSharp request including its resource and querystring parameters?

I.E for this 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);

I would like to get the full request URL:

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

回答1:


To get the full URL use RestClient.BuildUri()

Specifically, in this example use 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);



回答2:


Well, it is pretty tricky. To get the full requested URL use RestClient.Execute(request).ResponseUri to be sure it is the already sent request.

In this example:

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;

This code:

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

returns:

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


来源:https://stackoverflow.com/questions/41416362/restsharp-get-full-url-of-a-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!