csharp 瞬移
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 瞬移相关的知识,希望对你有一定的参考价值。
private ObjRespuesta EjecutarConsulta(Uri Uri, string Metodo, string Body, bool GuardarCookies, String ContentType = "")
{
var Resultado = new ObjRespuesta();
HttpWebRequest request = HttpWebRequest.Create(Uri) as HttpWebRequest;
request.Method = Metodo.ToString().ToUpper();
request.ServicePoint.Expect100Continue = false;
request.ContentType = ContentType;
request.Timeout = 999999999;
if (Metodo == "PATCH"){
WebHeaderCollection Header = new WebHeaderCollection();
Header.Add("B1S-ReplaceCollectionsOnPatch:true");
request.Headers = Header;
}
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
request.CookieContainer = new CookieContainer();
/*Definicion de Cookies*/
if (cookieData.Count > 0)
{
foreach (Cookie cookie in cookieData)
{
request.CookieContainer.Add(Uri, new Cookie(cookie.Name, cookie.Value));
}
}
/*Escribir el Body del Request*/
if (!string.IsNullOrEmpty(Body))
{
using (var requestStream = request.GetRequestStream())
{
var writer = new StreamWriter(requestStream);
writer.Write(Body);
writer.Close();
}
}
/*Obtener y procesar la respuesta*/
System.Net.HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
Resultado.exito = true;
Resultado.Contenido = reader.ReadToEnd();
}
if (GuardarCookies)
this.cookieData = response.Cookies;
response.Close();
}
catch (WebException ex)
{
if (ex.Response != null)
{
// can use ex.Response.Status, .StatusDescription
if (ex.Response.ContentLength != 0)
{
using (var stream = ex.Response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
try
{
if (ex.Response.ContentType == "application/json;charset=utf-8")
{
String Valor = reader.ReadToEnd();
if (Valor != "")
{
Resultado = JsonConvert.DeserializeObject<ObjRespuesta>(Valor);
}
else
{
Resultado.MsgError = "" + ex.Message;
}
}
else
{
Resultado.MsgError = "" + ex.Message;
}
}
catch (WebException ex2)
{
Resultado.MsgError = ex2.Message.ToString();
Resultado.exito = false;
}
//Resultado.Add(false, reader.ReadToEnd());
}
}
}
}
else
{
Resultado.MsgError = ex.Message;
}
}
return Resultado;
}
以上是关于csharp 瞬移的主要内容,如果未能解决你的问题,请参考以下文章