本文整理汇总了C#中RestSharp.HttpResponse类的典型用法代码示例。如果您正苦于以下问题:C# HttpResponse类的具体用法?C# HttpResponse怎么用?C# HttpResponse使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpResponse类属于RestSharp命名空间,在下文中一共展示了HttpResponse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ResponseCallback
private void ResponseCallback(IAsyncResult result, Action<HttpResponse> callback)
{
var response = new HttpResponse {ResponseStatus = ResponseStatus.None};
try
{
if(_timeoutState.TimedOut)
{
response.ResponseStatus = ResponseStatus.TimedOut;
ExecuteCallback(response, callback);
return;
}
GetRawResponseAsync(result, webResponse =>
{
ExtractResponseData(response, webResponse);
ExecuteCallback(response, callback);
});
}
catch(Exception ex)
{
ExecuteCallback(CreateErrorResponse(ex), callback);
}
}
示例2: DeleteService
public HttpResponse DeleteService(string serviceName)
{
XmlNode serviceNode = xmlDoc.SelectSingleNode("ServiceConfig/Service[@Name='" + serviceName + "']");
XmlNode keyNode = xmlDoc.SelectSingleNode("ServiceConfig/ApiKeys/Key[@Name='" + serviceName + "']");
if (serviceNode != null)
{
serviceNode.ParentNode.RemoveChild(serviceNode);
}
if (keyNode != null)
{
apiKeysNode.RemoveChild(keyNode);
}
xmlDoc.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs/ServiceConfig.xml"));
var res = new HttpResponse();
res.StatusCode = HttpStatusCode.OK;
return res; ;
}
示例3: ExtractResponseData
private static void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse)
{
using (webResponse)
{
#if FRAMEWORK
response.ContentEncoding = webResponse.ContentEncoding;
response.Server = webResponse.Server;
#endif
response.ContentType = webResponse.ContentType;
response.ContentLength = webResponse.ContentLength;
#if WINDOWS_PHONE
if (string.Equals(webResponse.Headers[HttpRequestHeader.ContentEncoding], "gzip", StringComparison.OrdinalIgnoreCase))
response.RawBytes = new GZipStream(webResponse.GetResponseStream()).ReadAsBytes();
else
response.RawBytes = webResponse.GetResponseStream().ReadAsBytes();
#else
response.RawBytes = webResponse.GetResponseStream().ReadAsBytes();
#endif
//response.Content = GetString(response.RawBytes);
response.StatusCode = webResponse.StatusCode;
response.StatusDescription = webResponse.StatusDescription;
response.ResponseUri = webResponse.ResponseUri;
response.ResponseStatus = ResponseStatus.Completed;
if (webResponse.Cookies != null)
{
foreach (Cookie cookie in webResponse.Cookies)
{
response.Cookies.Add(new HttpCookie {
Comment = cookie.Comment,
CommentUri = cookie.CommentUri,
Discard = cookie.Discard,
Domain = cookie.Domain,
Expired = cookie.Expired,
Expires = cookie.Expires,
HttpOnly = cookie.HttpOnly,
Name = cookie.Name,
Path = cookie.Path,
Port = cookie.Port,
Secure = cookie.Secure,
TimeStamp = cookie.TimeStamp,
Value = cookie.Value,
Version = cookie.Version
});
}
}
foreach (var headerName in webResponse.Headers.AllKeys)
{
var headerValue = webResponse.Headers[headerName];
response.Headers.Add(new HttpHeader { Name = headerName, Value = headerValue });
}
webResponse.Close();
}
}
示例4: RequestStreamCallback
private void RequestStreamCallback(IAsyncResult result, Action<HttpResponse> callback)
{
var webRequest = (HttpWebRequest)result.AsyncState;
if (_timeoutState.TimedOut)
{
var response = new HttpResponse {ResponseStatus = ResponseStatus.TimedOut};
ExecuteCallback(response, callback);
return;
}
// write body to request stream
try
{
using(var requestStream = webRequest.EndGetRequestStream(result))
{
if(HasFiles || AlwaysMultipartFormData)
{
WriteMultipartFormData(requestStream);
}
else if (RequestBodyBytes != null)
{
requestStream.Write(RequestBodyBytes, 0, RequestBodyBytes.Length);
}
else
{
WriteStringTo(requestStream, RequestBody);
}
}
}
catch (Exception ex)
{
ExecuteCallback(CreateErrorResponse(ex), callback);
return;
}
webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest);
}
示例5: ConvertToRestResponse
private RestResponse ConvertToRestResponse(IRestRequest request, HttpResponse httpResponse)
{
var restResponse = new RestResponse();
restResponse.Content = httpResponse.Content;
restResponse.ContentEncoding = httpResponse.ContentEncoding;
restResponse.ContentLength = httpResponse.ContentLength;
restResponse.ContentType = httpResponse.ContentType;
restResponse.ErrorException = httpResponse.ErrorException;
restResponse.ErrorMessage = httpResponse.ErrorMessage;
restResponse.RawBytes = httpResponse.RawBytes;
restResponse.ResponseStatus = httpResponse.ResponseStatus;
restResponse.ResponseUri = httpResponse.ResponseUri;
restResponse.Server = httpResponse.Server;
restResponse.StatusCode = httpResponse.StatusCode;
restResponse.StatusDescription = httpResponse.StatusDescription;
restResponse.Request = request;
foreach (var header in httpResponse.Headers)
{
restResponse.Headers.Add(new Parameter { Name = header.Name, Value = header.Value, Type = ParameterType.HttpHeader });
}
foreach (var cookie in httpResponse.Cookies)
{
restResponse.Cookies.Add(new RestResponseCookie {
Comment = cookie.Comment,
CommentUri = cookie.CommentUri,
Discard = cookie.Discard,
Domain = cookie.Domain,
Expired = cookie.Expired,
Expires = cookie.Expires,
HttpOnly = cookie.HttpOnly,
Name = cookie.Name,
Path = cookie.Path,
Port = cookie.Port,
Secure = cookie.Secure,
TimeStamp = cookie.TimeStamp,
Value = cookie.Value,
Version = cookie.Version
});
}
return restResponse;
}
示例6: ExtractResponseData
private void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse)
{
using (webResponse)
{
#if FRAMEWORK
response.ContentEncoding = webResponse.ContentEncoding;
response.Server = webResponse.Server;
#endif
response.ContentType = webResponse.ContentType;
response.ContentLength = webResponse.ContentLength;
Stream webResponseStream = webResponse.GetResponseStream();
#if WINDOWS_PHONE || UNITY
string contentEncoding = "";
if(webResponse.Headers[HttpResponseHeader.ContentEncoding] != null) {
contentEncoding = webResponse.Headers[HttpResponseHeader.ContentEncoding];
}
if (string.Equals(contentEncoding, "gzip", StringComparison.OrdinalIgnoreCase))
{
GZipStream gzStream = new GZipStream(webResponseStream);
ProcessResponseStream(gzStream, response);
}
else if (string.Equals(contentEncoding, "deflate", StringComparison.OrdinalIgnoreCase))
{
ZlibStream dfStream = new ZlibStream(webResponseStream);
ProcessResponseStream(dfStream, response);
}
else
{
ProcessResponseStream(webResponseStream, response);
}
#else
this.ProcessResponseStream(webResponseStream, response);
#endif
response.StatusCode = webResponse.StatusCode;
response.StatusDescription = webResponse.StatusDescription;
response.ResponseUri = webResponse.ResponseUri;
response.ResponseStatus = ResponseStatus.Completed;
if (webResponse.Cookies != null)
{
foreach (Cookie cookie in webResponse.Cookies)
{
response.Cookies.Add(new HttpCookie
{
Comment = cookie.Comment,
CommentUri = cookie.CommentUri,
Discard = cookie.Discard,
Domain = cookie.Domain,
Expired = cookie.Expired,
Expires = cookie.Expires,
HttpOnly = cookie.HttpOnly,
Name = cookie.Name,
Path = cookie.Path,
Port = cookie.Port,
Secure = cookie.Secure,
TimeStamp = cookie.TimeStamp,
Value = cookie.Value,
Version = cookie.Version
});
}
}
foreach (string headerName in webResponse.Headers.AllKeys)
{
string headerValue = webResponse.Headers[headerName];
response.Headers.Add(new HttpHeader
{
Name = headerName,
Value = headerValue
});
}
webResponse.Close();
}
}
示例7: ResponseCallback
private void ResponseCallback(IAsyncResult result, Action<HttpResponse> callback)
{
var response = new HttpResponse();
response.ResponseStatus = ResponseStatus.None;
try
{
if (timeoutState.TimedOut)
{
response.ResponseStatus = ResponseStatus.TimedOut;
ExecuteCallback(response, callback);
return;
}
GetRawResponseAsync(result, webResponse =>
{
ExtractResponseData(response, webResponse);
ExecuteCallback(response, callback);
});
}
catch (Exception ex)
{
response.ErrorMessage = ex.Message;
response.ErrorException = ex;
response.ResponseStatus = ResponseStatus.Error;
ExecuteCallback(response, callback);
}
}
示例8: ExtractErrorResponse
private void ExtractErrorResponse(HttpResponse httpResponse, Exception ex)
{
WebException webException = ex as WebException;
if (webException != null && webException.Status == WebExceptionStatus.Timeout)
{
httpResponse.ResponseStatus = ResponseStatus.TimedOut;
httpResponse.ErrorMessage = ex.Message;
httpResponse.ErrorException = webException;
return;
}
httpResponse.ErrorMessage = ex.Message;
httpResponse.ErrorException = ex;
httpResponse.ResponseStatus = ResponseStatus.Error;
}
示例9: GetStyleMethodInternalAsync
private void GetStyleMethodInternalAsync(string method, Action<HttpResponse> callback)
{
try
{
var url = Url;
var webRequest = ConfigureAsyncWebRequest(method, url);
timeoutState = new TimeOutState { Request = webRequest };
var asyncResult = webRequest.BeginGetResponse(result => ResponseCallback(result, callback), webRequest);
SetTimeout(asyncResult, webRequest, timeoutState);
}
catch (Exception ex)
{
var response = new HttpResponse();
response.ErrorMessage = ex.Message;
response.ErrorException = ex;
response.ResponseStatus = ResponseStatus.Error;
ExecuteCallback(response, callback);
}
}
示例10: PutPostInternalAsync
private void PutPostInternalAsync(string method, Action<HttpResponse> callback)
{
try
{
var webRequest = ConfigureAsyncWebRequest(method, Url);
PreparePostBody(webRequest);
WriteRequestBodyAsync(webRequest, callback);
}
catch (Exception ex)
{
var response = new HttpResponse();
response.ErrorMessage = ex.Message;
response.ErrorException = ex;
response.ResponseStatus = ResponseStatus.Error;
ExecuteCallback(response, callback);
}
}
示例11: GetRawResponseAsync
private void GetRawResponseAsync(IAsyncResult result, Action<HttpWebResponse> callback)
{
var response = new HttpResponse();
response.ResponseStatus = ResponseStatus.None;
HttpWebResponse raw = null;
try
{
var webRequest = (HttpWebRequest)result.AsyncState;
raw = webRequest.EndGetResponse(result) as HttpWebResponse;
}
catch (WebException ex)
{
if (ex.Response is HttpWebResponse)
{
raw = ex.Response as HttpWebResponse;
}
}
callback(raw);
raw.Close();
}
示例12: ExecuteCallback
private void ExecuteCallback(HttpResponse response, Action<HttpResponse> callback)
{
#if WINDOWS_PHONE
var dispatcher = Deployment.Current.Dispatcher;
dispatcher.BeginInvoke(() =>
{
#endif
callback(response);
#if WINDOWS_PHONE
});
#endif
}
示例13: PerformGetResponse
private HttpResponse PerformGetResponse(string method)
{
var response = new HttpResponse();
response.ResponseStatus = ResponseStatus.None;
try
{
Assert.InRange(CurrentCallNumber, 0, GetResponses.Count - 1);
var r = GetResponses[CurrentCallNumber](method);
CurrentCallNumber++;
var bytes = r.Raw ? (byte[])r.ResponseObject : Encoding.UTF8.GetBytes(new BaseXmlSerializer().Serialize(r.ResponseObject));
response.ResponseStatus = ResponseStatus.Completed;
response.StatusCode = r.StatusCode;
response.ContentType = r.ContentType ?? "text/xml";
response.ContentLength = bytes.Length;
response.RawBytes = bytes;
}
catch (Exception ex)
{
response.ErrorMessage = ex.Message;
response.ErrorException = ex;
response.ResponseStatus = ResponseStatus.Error;
}
Reset();
return response;
}
示例14: ExtractResponseData
private void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse)
{
using (webResponse)
{
response.ContentType = webResponse.ContentType;
response.ContentLength = webResponse.ContentLength;
Stream webResponseStream = webResponse.GetResponseStream();
ProcessResponseStream(webResponseStream, response);
response.StatusCode = webResponse.StatusCode;
response.StatusDescription = webResponse.StatusDescription;
response.ResponseUri = webResponse.ResponseUri;
response.ResponseStatus = ResponseStatus.Completed;
if (webResponse.Cookies != null)
{
foreach (Cookie cookie in webResponse.Cookies)
{
response.Cookies.Add(new HttpCookie {
Comment = cookie.Comment,
CommentUri = cookie.CommentUri,
Discard = cookie.Discard,
Domain = cookie.Domain,
Expired = cookie.Expired,
Expires = cookie.Expires,
HttpOnly = cookie.HttpOnly,
Name = cookie.Name,
Path = cookie.Path,
Port = cookie.Port,
Secure = cookie.Secure,
TimeStamp = cookie.TimeStamp,
Value = cookie.Value,
Version = cookie.Version
});
}
}
foreach (var headerName in webResponse.Headers.AllKeys)
{
var headerValue = webResponse.Headers[headerName];
response.Headers.Add(new HttpHeader { Name = headerName, Value = headerValue });
}
webResponse.Close();
}
}
示例15: GetResponse
// stripped down version of rest-sharp implementation
private HttpResponse GetResponse(WebRequest request)
{
var response = new HttpResponse { ResponseStatus = ResponseStatus.None };
try
{
var webResponse = GetRawResponse(request);
ExtractResponseData(response, webResponse);
}
catch (Exception ex)
{
response.ErrorMessage = ex.Message;
response.ErrorException = ex;
response.ResponseStatus = ResponseStatus.Error;
}
return response;
}