本文整理汇总了C#中RestSharp类的典型用法代码示例。如果您正苦于以下问题:C# RestSharp类的具体用法?C# RestSharp怎么用?C# RestSharp使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RestSharp类属于命名空间,在下文中一共展示了RestSharp类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Request
public string Request(RestSharp.Method method, string endPoint, Dictionary<string,object> headers, Dictionary<string, object> parameters, Dictionary<string, object> queryParameters, string body)
{
RestClient client = new RestClient(baseURL);
RestRequest request = new RestRequest(endPoint, method);
client.Authenticator = authenticator;
foreach (var key in headers.Keys)
{
if(headers[key].GetType().ToString().StartsWith("System.Collections.Generics.List"))
{
request.AddHeader(key,JsonConvert.SerializeObject(headers[key]));
}
else
{
request.AddHeader(key,headers[key].ToString());
}
}
foreach (var key in parameters.Keys)
{
request.AddParameter(key, parameters[key]);
}
foreach (var key in queryParameters.Keys)
{
if (headers[key].GetType().ToString().StartsWith("System.Collections.Generics.List"))
{
request.AddQueryParameter(key, JsonConvert.SerializeObject(queryParameters[key]));
}
else
{
request.AddQueryParameter(key, queryParameters[key].ToString());
}
}
var response = client.Execute(request);
return response.Content;
}
示例2: CallApi
/// <summary>
/// Makes the HTTP request (Sync).
/// </summary>
/// <param name="path">URL path.</param>
/// <param name="method">HTTP method.</param>
/// <param name="queryParams">Query parameters.</param>
/// <param name="postBody">HTTP body (POST request).</param>
/// <param name="headerParams">Header parameters.</param>
/// <param name="formParams">Form parameters.</param>
/// <param name="fileParams">File parameters.</param>
/// <param name="authSettings">Authentication settings.</param>
/// <returns>Object</returns>
public Object CallApi(String path, RestSharp.Method method, Dictionary<String, String> queryParams, String postBody,
Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
Dictionary<String, FileParameter> fileParams, String[] authSettings)
{
var request = new RestRequest(path, method);
UpdateParamsForAuth(queryParams, headerParams, authSettings);
// add default header, if any
foreach(var defaultHeader in _defaultHeaderMap)
request.AddHeader(defaultHeader.Key, defaultHeader.Value);
// add header parameter, if any
foreach(var param in headerParams)
request.AddHeader(param.Key, param.Value);
// add query parameter, if any
foreach(var param in queryParams)
request.AddParameter(param.Key, param.Value, ParameterType.GetOrPost);
// add form parameter, if any
foreach(var param in formParams)
request.AddParameter(param.Key, param.Value, ParameterType.GetOrPost);
// add file parameter, if any
foreach(var param in fileParams)
request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType);
if (postBody != null) // http body (model) parameter
request.AddParameter("application/json", postBody, ParameterType.RequestBody);
return (Object)RestClient.Execute(request);
}
示例3: CallApiAsync
public async Task<Object> CallApiAsync(String Path, RestSharp.Method Method, Dictionary<String, String> QueryParams, String PostBody,
Dictionary<String, String> HeaderParams, Dictionary<String, String> FormParams, Dictionary<String, String> FileParams, String[] AuthSettings) {
var request = new RestRequest(Path, Method);
UpdateParamsForAuth(QueryParams, HeaderParams, AuthSettings);
// add default header, if any
foreach(KeyValuePair<string, string> defaultHeader in this.defaultHeaderMap)
request.AddHeader(defaultHeader.Key, defaultHeader.Value);
// add header parameter, if any
foreach(KeyValuePair<string, string> param in HeaderParams)
request.AddHeader(param.Key, param.Value);
// add query parameter, if any
foreach(KeyValuePair<string, string> param in QueryParams)
request.AddQueryParameter(param.Key, param.Value);
// add form parameter, if any
foreach(KeyValuePair<string, string> param in FormParams)
request.AddParameter(param.Key, param.Value);
// add file parameter, if any
foreach(KeyValuePair<string, string> param in FileParams)
request.AddFile(param.Key, param.Value);
if (PostBody != null) {
request.AddParameter("application/json", PostBody, ParameterType.RequestBody); // http body (model) parameter
}
return (Object) await restClient.ExecuteTaskAsync(request);
}
示例4: PrepareRequest
public override void PrepareRequest(RestSharp.RestRequest req)
{
req.AddParameter("clientString", _clientString, ParameterType.GetOrPost);
req.AddParameter("client_id", _clientId, ParameterType.GetOrPost);
req.AddParameter("userLogin", _username, ParameterType.GetOrPost);
req.AddParameter("password", _password, ParameterType.GetOrPost);
}
示例5: PrepareRequest
// Creates and sets up a RestRequest prior to a call.
private RestRequest PrepareRequest(
String path, RestSharp.Method method, Dictionary<String, String> queryParams, String postBody,
Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams)
{
var request = new RestRequest(path, method);
// add path parameter, if any
foreach(var param in pathParams)
request.AddParameter(param.Key, param.Value, ParameterType.UrlSegment);
// add header parameter, if any
foreach(var param in headerParams)
request.AddHeader(param.Key, param.Value);
// add query parameter, if any
foreach(var param in queryParams)
request.AddQueryParameter(param.Key, param.Value);
// add form parameter, if any
foreach(var param in formParams)
request.AddParameter(param.Key, param.Value);
// add file parameter, if any
foreach(var param in fileParams)
request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType);
if (postBody != null) // http body (model) parameter
request.AddParameter("application/json", postBody, ParameterType.RequestBody);
return request;
}
示例6: ToHttpResponse
public SDK.Http.IHttpResponse ToHttpResponse(RestSharp.IRestResponse response)
{
bool transportError = false;
var responseMessages = new List<string>();
transportError =
response.ResponseStatus == RestSharp.ResponseStatus.TimedOut ||
response.ResponseStatus == RestSharp.ResponseStatus.Aborted;
if (response.ErrorException != null)
{
responseMessages.Add(response.ErrorException.Message);
}
else if (!string.IsNullOrEmpty(response.ErrorMessage))
{
responseMessages.Add(response.ErrorMessage);
}
if (!string.IsNullOrEmpty(response.StatusDescription))
{
responseMessages.Add(response.StatusDescription);
}
var headers = this.ToHttpHeaders(response.Headers);
return new Impl.Http.DefaultHttpResponse(
(int)response.StatusCode,
string.Join(Environment.NewLine, responseMessages),
headers,
response.Content,
response.ContentType,
transportError);
}
示例7: CallApi
public Object CallApi(String Path, RestSharp.Method Method, Dictionary<String, String> QueryParams, String PostBody,
Dictionary<String, String> HeaderParams, Dictionary<String, String> FormParams, Dictionary<String, String> FileParams, String[] AuthSettings) {
var response = Task.Run(async () => {
var resp = await CallApiAsync(Path, Method, QueryParams, PostBody, HeaderParams, FormParams, FileParams, AuthSettings);
return resp;
});
return response.Result;
}
示例8: Authenticate
/// <summary>
/// Adds the API token key to the REST request header
/// </summary>
public void Authenticate(RestSharp.IRestClient client, RestSharp.IRestRequest request)
{
if (!request.Parameters.Any(p => p.Name.Equals("X-TOKEN", StringComparison.OrdinalIgnoreCase)))
{
var token = string.Format("{0}", _apiToken);
request.AddParameter("X-TOKEN", token, ParameterType.HttpHeader);
}
}
示例9: Execute
protected RestSharp.IRestResponse Execute(string url, RestSharp.Method method, List<Header> headers)
{
if (headers == null) headers = new List<Header>();
RestSharp.IRestClient client = new RestSharp.RestClient(Root);
RestSharp.IRestRequest request = new RestSharp.RestRequest(url, method);
foreach (var header in headers)
{
request.AddParameter(header.Name, header.Value, RestSharp.ParameterType.HttpHeader);
}
request.RequestFormat = RestSharp.DataFormat.Json;
var response = client.Execute(request);
return response;
}
示例10: CopyHeaders
private void CopyHeaders(SDK.Http.HttpHeaders httpHeaders, RestSharp.IRestRequest restRequest)
{
if (httpHeaders == null)
{
return;
}
foreach (var header in httpHeaders)
{
foreach (var value in header.Value)
{
restRequest.AddHeader(header.Key, value);
}
}
}
示例11: CheckException
internal bool CheckException(RestSharp.IRestResponse response)
{
if (response.ErrorException != null)
{
_log.Error(string.Format("Response error. StockID: {0}, Date: {1}", MineCar.StockID, MineCar.MiningDate), response.ErrorException);
return false;
}
else if (!response.RawBytes.Any())
{
_log.ErrorFormat("RawBytes is empty. StockID: {0}, Date: {1}", MineCar.StockID, MineCar.MiningDate);
return false;
}
return true;
}
示例12: Request
public RestAPIResponse Request(string endpoint, dynamic parameters = null, RestSharp.Method method = Method.GET)
{
var request = new RestRequest(endpoint, method);
RestAPIResponse result = null;
if (parameters != null) request.Parameters.AddRange(APIHelpers.ParseParameters(endpoint, parameters));
IRestResponse response = _client.Execute(request);
if(endpoint == "pull")
{
result = new PullAPIResponse() { RateLimit = APIHelpers.ParseRateLimitHeaders(response.Headers), StatusCode = response.StatusCode, PullDetails = APIHelpers.ParsePullDetailHeaders(response.Headers) };
result.Data = APIHelpers.DeserializeResponse(response.Content, ((PullAPIResponse)result).PullDetails.Format);
}
else
{
result = new RestAPIResponse() { RateLimit = APIHelpers.ParseRateLimitHeaders(response.Headers), StatusCode = response.StatusCode };
result.Data = APIHelpers.DeserializeResponse(response.Content);
}
switch((int)response.StatusCode)
{
// Ok status codes
case 200:
case 201:
case 202:
case 204:
break;
//Error status codes
case 400:
case 401:
case 403:
case 404:
case 405:
case 409:
case 413:
case 416:
case 500:
case 503:
throw new RestAPIException(result, (APIHelpers.HasAttr(result.Data, "error")) ? result.Data.error : "The request failed, please see the Data & StatusCode properties for more details.");
}
return result;
}
示例13: Request
private string Request(string path, RestSharp.Method method, string Data)
{
var request = new RestRequest(path, method);
request.AddParameter("text/plain", Data, ParameterType.RequestBody);
RestResponse response = Cli.Execute(request);
var code = response.StatusCode;
if(code == HttpStatusCode.Created || code == HttpStatusCode.OK || code == HttpStatusCode.NoContent)
{
return response.Content;
}
else
{
string ErrorMessage = Json.Deserialize<CoconutError> (response.Content).Message;
throw new CoconutException(ErrorMessage);
}
}
示例14: WarmupRequest
// This is utility function used to set Request Header and Request Parameters.
private void WarmupRequest(RestSharp.Method currentMethod,string host,string requestURL
, Dictionary<string,string> parameters, Dictionary<string,string> header)
{
restClient = new RestClient(host);
restRequest = new RestRequest(requestURL, currentMethod);
if(parameters!=null)
foreach (string paramKey in parameters.Keys)
{
restRequest.AddParameter(paramKey,parameters[paramKey]);
}
if(header!= null)
foreach (string headerKey in header.Keys)
{
restRequest.AddParameter(headerKey, parameters[headerKey]);
}
}
示例15: PrepareRequest
// Creates and sets up a RestRequest prior to a call.
protected virtual RestRequest PrepareRequest(
String path, RestSharp.Method method, Dictionary<String, String> queryParams, Object postBody,
Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams,
String contentType)
{
var request = new RestRequest(path, method);
// add path parameter, if any
foreach(var param in pathParams)
request.AddParameter(param.Key, param.Value, ParameterType.UrlSegment);
// add header parameter, if any
foreach(var param in headerParams)
request.AddHeader(param.Key, param.Value);
// add query parameter, if any
foreach(var param in queryParams)
request.AddQueryParameter(param.Key, param.Value);
// add form parameter, if any
foreach(var param in formParams)
request.AddParameter(param.Key, param.Value);
// add file parameter, if any
foreach(var param in fileParams)
{
request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType);
}
if (postBody != null) // http body (model or byte[]) parameter
{
if (postBody.GetType() == typeof(String))
{
request.AddParameter("application/json", postBody, ParameterType.RequestBody);
}
else if (postBody.GetType() == typeof(byte[]))
{
request.AddParameter(contentType, postBody, ParameterType.RequestBody);
}
}
return request;
}