2017-01-01 135 views
8

有沒有辦法獲得RestSharp請求的完整url,包括它的資源和querystring參數?RestSharp獲取請求的完整URL

IE對於這個請求:

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); 

我想獲得完整的請求URL:

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

回答

18

要獲得完整的URL中使用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); 
+0

這很好,但我注意到根據我的經驗,你可以在Execute()之前調用'BuildUri()',這似乎更自然(我想記錄我要做的事情,而不是我剛做的事情) 。 –