本文整理汇总了C#中RestClient.Execute方法的典型用法代码示例。如果您正苦于以下问题:C# RestClient.Execute方法的具体用法?C# RestClient.Execute怎么用?C# RestClient.Execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RestClient
的用法示例。
在下文中一共展示了RestClient.Execute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestMultipleRequests
public async Task TestMultipleRequests(Type factoryType)
{
using (var client = new RestClient("http://httpbin.org/")
{
HttpClientFactory = CreateClientFactory(factoryType, false),
})
{
{
var request = new RestRequest("post", Method.POST);
request.AddParameter("param1", "param1");
var response = await client.Execute<HttpBinResponse>(request);
Assert.NotNull(response.Data);
Assert.NotNull(response.Data.Form);
Assert.True(response.Data.Form.ContainsKey("param1"));
Assert.Equal("param1", response.Data.Form["param1"]);
}
{
var request = new RestRequest("post", Method.POST);
request.AddParameter("param1", "param1+");
var response = await client.Execute<HttpBinResponse>(request);
Assert.NotNull(response.Data);
Assert.NotNull(response.Data.Form);
Assert.True(response.Data.Form.ContainsKey("param1"));
Assert.Equal("param1+", response.Data.Form["param1"]);
}
}
}
示例2: Can_Authenticate_With_OAuth
//[Test]
public void Can_Authenticate_With_OAuth()
{
Uri baseUrl = new Uri("https://api.twitter.com");
RestClient client = new RestClient(baseUrl)
{
Authenticator = OAuth1Authenticator.ForRequestToken("CONSUMER_KEY", "CONSUMER_SECRET")
};
RestRequest request = new RestRequest("oauth/request_token");
IRestResponse response = client.Execute(request);
Assert.NotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
NameValueCollection qs = HttpUtility.ParseQueryString(response.Content);
string oauthToken = qs["oauth_token"];
string oauthTokenSecret = qs["oauth_token_secret"];
Assert.NotNull(oauthToken);
Assert.NotNull(oauthTokenSecret);
request = new RestRequest("oauth/authorize?oauth_token=" + oauthToken);
string url = client.BuildUri(request)
.ToString();
Process.Start(url);
const string verifier = "123456"; // <-- Breakpoint here (set verifier in debugger)
request = new RestRequest("oauth/access_token");
client.Authenticator = OAuth1Authenticator.ForAccessToken(
"P5QziWtocYmgWAhvlegxw", "jBs07SIxJ0kodeU9QtLEs1W1LRgQb9u5Lc987BA94", oauthToken,
oauthTokenSecret, verifier);
response = client.Execute(request);
Assert.NotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
qs = HttpUtility.ParseQueryString(response.Content);
oauthToken = qs["oauth_token"];
oauthTokenSecret = qs["oauth_token_secret"];
Assert.NotNull(oauthToken);
Assert.NotNull(oauthTokenSecret);
request = new RestRequest("account/verify_credentials.xml");
client.Authenticator = OAuth1Authenticator.ForProtectedResource(
"P5QziWtocYmgWAhvlegxw", "jBs07SIxJ0kodeU9QtLEs1W1LRgQb9u5Lc987BA94", oauthToken,
oauthTokenSecret);
response = client.Execute(request);
Assert.NotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
示例3: Can_Authenticate_With_OAuth
public void Can_Authenticate_With_OAuth()
{
const string consumerKey = "";
const string consumerSecret = "";
var baseUrl = "https://api.twitter.com";
var client = new RestClient(baseUrl);
client.Authenticator = OAuth1Authenticator.ForRequestToken(consumerKey, consumerSecret);
var request = new RestRequest("oauth/request_token", Method.POST);
var response = client.Execute(request);
Assert.NotNull(response);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var qs = HttpUtility.ParseQueryString(response.Content);
var oauth_token = qs["oauth_token"];
var oauth_token_secret = qs["oauth_token_secret"];
Assert.NotNull(oauth_token);
Assert.NotNull(oauth_token_secret);
request = new RestRequest("oauth/authorize");
request.AddParameter("oauth_token", oauth_token);
var url = client.BuildUri(request).ToString();
Process.Start(url);
var verifier = "123456"; // <-- Breakpoint here (set verifier in debugger)
request = new RestRequest("oauth/access_token");
client.Authenticator = OAuth1Authenticator.ForAccessToken(
consumerKey, consumerSecret, oauth_token, oauth_token_secret, verifier
);
response = client.Execute(request);
Assert.NotNull(response);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
qs = HttpUtility.ParseQueryString(response.Content);
oauth_token = qs["oauth_token"];
oauth_token_secret = qs["oauth_token_secret"];
Assert.NotNull(oauth_token);
Assert.NotNull(oauth_token_secret);
request = new RestRequest("account/verify_credentials.xml");
client.Authenticator = OAuth1Authenticator.ForProtectedResource(
consumerKey, consumerSecret, oauth_token, oauth_token_secret
);
response = client.Execute(request);
Assert.NotNull(response);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
示例4: MainAsync
async static Task MainAsync(string[] args, CancellationTokenSource cts)
{
using (RestClient client = new RestClient("http://localhost:4711") { Timeout = new TimeSpan(0, 0, 10) })
{
while (!cts.Token.IsCancellationRequested)
{
if (Console.KeyAvailable)
{
var key = Console.ReadKey();
switch (key.KeyChar)
{
case 'a':
{
RestRequest request = new RestRequest("Hello", RestSharp.Portable.Method.POST);
request.AddJsonBody(new HelloRequest
{
Text = "HelloText",
Id = 1,
});
var response = await client.Execute<HelloResponse>(request);
Console.WriteLine($"Hello Response Text={response.Data.Text} Id={response.Data.Id}");
break;
}
case'b':
{
RestRequest request = new RestRequest("User/{id}", RestSharp.Portable.Method.GET);
request.AddUrlSegment("id", 1);
var response = await client.Execute<string>(request);
Console.WriteLine($"User Response Text={Encoding.UTF8.GetString(response.RawBytes)}");
break;
}
case 'c':
{
RestRequest request = new RestRequest("User/{id}", RestSharp.Portable.Method.GET);
request.AddUrlSegment("id", 666);
var response = await client.Execute(request);
Console.WriteLine($"User Response Text={Encoding.UTF8.GetString(response.RawBytes)}");
break;
}
case '\n':
cts.Cancel();
return;
}
}
else
Thread.Sleep(100);
}
}
}
示例5: Execute
/// <summary>
/// Retrieves a new token from Webtrends auth service
/// </summary>
public string Execute()
{
var builder = new JWTBuilder();
var header = new JWTHeader
{
Type = "JWT",
Algorithm = "HS256"
};
var claimSet = new JWTClaimSet
{
Issuer = clientId,
Principal = clientId,
Audience = audience,
Expiration = DateTime.Now.ToUniversalTime().AddSeconds(30),
Scope = scope
};
string assertion = builder.BuildAssertion(header, claimSet, clientSecret);
var client = new RestClient(authUrl);
var request = new RestRequest("token/", Method.POST);
request.AddParameter("client_id", clientId);
request.AddParameter("client_assertion", assertion);
request.AddParameter("grant_type", "client_credentials");
request.AddParameter("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
var response = client.Execute(request).Content;
return (string)JObject.Parse(response)["access_token"];
}
示例6: Handles_Different_Root_Element_On_Http_Error
public void Handles_Different_Root_Element_On_Http_Error()
{
Uri baseUrl = new Uri("http://localhost:8888/");
using (SimpleServer.Create(baseUrl.AbsoluteUri, Handlers.Generic<ResponseHandler>()))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("error")
{
RootElement = "Success"
};
request.OnBeforeDeserialization = resp =>
{
if (resp.StatusCode == HttpStatusCode.BadRequest)
{
request.RootElement = "Error";
}
};
IRestResponse<Response> response = client.Execute<Response>(request);
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
Assert.AreEqual("Not found!", response.Data.Message);
}
}
示例7: GetPublicationsIds
public PubMedPublicationIdsResult GetPublicationsIds(PubMedQueryFilter filter)
{
var restClient = new RestClient(ServiceURLs.ESearchBaseURL);
var restRequest = new RestRequest();
restRequest.AddParameter("db", databaseName, ParameterType.QueryString);
restRequest.AddParameter("retmode", "json", ParameterType.QueryString);
restRequest.AddParameter("retstart", (filter.Skip * filter.Take), ParameterType.QueryString);
restRequest.AddParameter("term", filter.Query, ParameterType.QueryString);
restRequest.AddParameter("retmax", filter.Take, ParameterType.QueryString);
if (filter.RelDate != DateTime.MinValue)
{
var pmDate = PubMedDateOperations.DatetimeToPubMedDate(filter.RelDate);
restRequest.AddParameter("reldate", pmDate, ParameterType.QueryString);
}
var waitTime = PubMedThrottler.GetWaitTime();
Thread.Sleep(waitTime);
var response = restClient.Execute<PubMedResponse>(restRequest).Result;
if (response.Data == null)
throw new Exception("No Response From The Server");
var result = new PubMedPublicationIdsResult();
result.PubMedIdCollection = new List<string>();
response.Data.esearchresult.idlist.ForEach(r => result.PubMedIdCollection.Add(r));
return result;
}
示例8: Handles_GET_Request_404_Error
public void Handles_GET_Request_404_Error()
{
var client = new RestClient(BaseUrl);
var request = new RestRequest("StatusCode/404");
var response = client.Execute(request);
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
示例9: Can_Handle_Uncompressed_Content
public void Can_Handle_Uncompressed_Content()
{
var client = new RestClient(BaseUrl);
var request = new RestRequest("Compression/None");
var response = client.Execute(request);
Assert.Equal("This content is uncompressed!", response.Content);
}
示例10: Handles_Non_Existent_Domain
public void Handles_Non_Existent_Domain()
{
var client = new RestClient("http://nonexistantdomainimguessing.org");
var request = new RestRequest("foo");
var response = client.Execute(request);
Assert.Equal(ResponseStatus.Error, response.ResponseStatus);
}
示例11: Can_Handle_Gzip_Compressed_Content
public void Can_Handle_Gzip_Compressed_Content()
{
var client = new RestClient(BaseUrl);
var request = new RestRequest("Compression/GZip");
var response = client.Execute(request);
Assert.Equal("This content is compressed with GZip!", response.Content);
}
示例12: ShouldParseOutRangeSpecifier
public void ShouldParseOutRangeSpecifier()
{
var restClient = new RestClient("http://localhost");
var req = new RestRequest("bob", Method.GET);
req.AddHeader("Range", "pages=1-2");
var resp = restClient.Execute(req);
}
示例13: Can_Authenticate_LinkedIN_With_OAuth
public void Can_Authenticate_LinkedIN_With_OAuth()
{
const string consumerKey = "TODO_CONSUMER_KEY_HERE";
const string consumerSecret = "TODO_CONSUMER_SECRET_HERE";
// request token
var client = new RestClient {
BaseUrl = "https://api.linkedin.com/uas/oauth",
Authenticator = OAuth1Authenticator.ForRequestToken( consumerKey, consumerSecret, "http://localhost" )
};
var requestTokenRequest = new RestRequest( "requestToken" );
var requestTokenResponse = client.Execute( requestTokenRequest );
Assert.NotNull( requestTokenResponse );
Assert.Equal( HttpStatusCode.OK, requestTokenResponse.StatusCode );
var requestTokenResponseParameters = HttpUtility.ParseQueryString( requestTokenResponse.Content );
var requestToken = requestTokenResponseParameters[ "oauth_token" ];
var requestSecret = requestTokenResponseParameters[ "oauth_token_secret" ];
Assert.NotNull( requestToken );
Assert.NotNull( requestSecret );
// redirect user
requestTokenRequest = new RestRequest( "authenticate?oauth_token=" + requestToken );
var redirectUri = client.BuildUri( requestTokenRequest );
Process.Start( redirectUri.ToString() );
var requestUrl = "TODO: put browser URL here"; // replace this via the debugger with the return url from LinkedIN. Simply copy it from the opened browser
if ( !Debugger.IsAttached )
{
Debugger.Launch();
}
Debugger.Break();
// get the access token
var requestTokenQueryParameters = HttpUtility.ParseQueryString( new Uri( requestUrl ).Query );
var requestVerifier = requestTokenQueryParameters[ "oauth_verifier" ];
client.Authenticator = OAuth1Authenticator.ForAccessToken( consumerKey, consumerSecret, requestToken, requestSecret, requestVerifier );
var requestAccessTokenRequest = new RestRequest( "accessToken" );
var requestActionTokenResponse = client.Execute( requestAccessTokenRequest );
Assert.NotNull( requestActionTokenResponse );
Assert.Equal( HttpStatusCode.OK, requestActionTokenResponse.StatusCode );
var requestActionTokenResponseParameters = HttpUtility.ParseQueryString( requestActionTokenResponse.Content );
var accessToken = requestActionTokenResponseParameters[ "oauth_token" ];
var accessSecret = requestActionTokenResponseParameters[ "oauth_token_secret" ];
Assert.NotNull( accessToken );
Assert.NotNull( accessSecret );
}
示例14: Can_Authenticate_With_Basic_Http_Auth
public void Can_Authenticate_With_Basic_Http_Auth()
{
var client = new RestClient(BaseUrl);
client.Authenticator = new HttpBasicAuthenticator("testuser", "testpassword");
var request = new RestRequest("Authentication/Basic");
var response = client.Execute(request);
Assert.Equal("testuser|testpassword", response.Content);
}
示例15: PUT_with_no_params_returns_411
public void PUT_with_no_params_returns_411()
{
var request = new RestRequest("Dump", Method.PUT);
var client = new RestClient(BaseUrl);
var response = client.Execute(request);
Assert.Equal("0", response.Content);
}