/// <summary>
/// 提交POST请求
/// </summary>
/// <param name="url">提交地址</param>
/// <param name="postData">提交的数据</param>
/// <param name="sendEncode">发送时使用的编码格式</param>
/// <param name="receiveEncode">接收时使用的编码格式</param>
/// <param name="timeout">超时时间</param>
/// <param name="signFlag">是否签名</param>
/// <param name="caLogin">是否为登录,如果是登录通过post提交数据需要的+进行替换</param>
/// <returns></returns>
private static CResponseResult HttpPostResult(String url, String postData, Encoding sendEncode, Encoding receiveEncode, int timeout = , bool signFlag = false,bool caLogin=false)
{
using (new CWaitCursor())
{ //连接网站是否出错
bool isConnErr = false;
//返回结果
CResponseResult result = new CResponseResult();
//创建Http请求
HttpWebRequest request;
try
{
byte[] postBytes = null; if (caLogin)
{
postBytes = sendEncode.GetBytes(postData.Replace("+", "%2B"));
}
else
{
postBytes = sendEncode.GetBytes(postData);
} //设置用于验证服务器证书的回调
ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
//创建Http请求
//如果是发送HTTPS请求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
}
else
request = (HttpWebRequest)WebRequest.Create(new Uri(url));
//超时
if (timeout > )
{
//此处是毫秒为单位,不能随便转成以秒单位
request.Timeout = timeout;
if (request.ReadWriteTimeout < request.Timeout)
request.ReadWriteTimeout = request.Timeout;
}
request.CookieContainer = GetCookie(url);
request.Headers.Set("Cache-Control", "no-cache");
request.Headers.Set("Pragma", "no-cache");
request.Headers.Set("FRAME_SSO_TICKET", LscSSOTicket.Replace("\r","").Replace("\n","")); request.Method = "POST"; if (signFlag)
{
if (Encoding.Default.GetBytes(postData).Length > limitSize * )
{
string compressDataString = BZipUtil.compressTobase64(BZipUtil.compress(postData));
postBytes = sendEncode.GetBytes(compressDataString);
request.ContentType = "application/zip-compress-ca-encrypted-data";
}
else
{
request.ContentType = "application/ca-encrypted-data";
}
}
else
{
if (Encoding.Default.GetBytes(postData).Length > limitSize * )
{
string compressDataString = BZipUtil.compressTobase64(BZipUtil.compress(postData));
postBytes = sendEncode.GetBytes(compressDataString);
request.ContentType = "application/zip-compress-data";
}
else
{
request.ContentType = "application/x-www-form-urlencoded";
}
} request.ContentLength = postBytes.Length;
//发起Http请求 //设置代理
ProxySetting(request); //用于发送数据的 Stream 对象
Stream requestStream = request.GetRequestStream();
requestStream.Write(postBytes, , postBytes.Length);
requestStream.Close(); //if (currBaseCookie == null)
//{
// currBaseCookie = new CookieContainer();
//request.CookieContainer = GetCookie(url);
//}
}
catch (Exception ex)
{
isConnErr = true;
result.Code = "";
result.Message = ex.Message;
//出现异常,统一抛出HttpPostException
throw new HttpPostException(ex);
} //Http响应
if (!isConnErr)
{
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Encoding jspEncode = GetJspEncode(response.GetResponseHeader("Content-Type"));
string contentType = response.ContentType;
StreamReader reader = new StreamReader(response.GetResponseStream(), jspEncode == null ? receiveEncode : jspEncode);
if (contentType.Contains("application/zip-compress-data;"))
{
//返回内容
result.Message = BZipUtil.decompress(BZipUtil.base64Tocompress(reader.ReadToEnd().Trim()));
}
else
{
result.Message = reader.ReadToEnd().Trim();
}
result.Code = "";
//关闭连接
reader.Close();
response.Close();
}
catch (WebException ex)
{
HttpWebResponse res = ex.Response as HttpWebResponse;
if (res != null)
{
if (res.StatusCode == HttpStatusCode.Forbidden)
{
Stream s = res.GetResponseStream();
Encoding jspEncode = GetJspEncode(res.GetResponseHeader("Content-Type"));
string contentType = res.ContentType;
StreamReader objReader = new StreamReader(s, receiveEncode);
if (contentType.Contains("application/zip-compress-data;"))
{
//返回内容
result.Message = BZipUtil.decompress(BZipUtil.base64Tocompress(objReader.ReadToEnd().Trim()));
}
else
{
result.Message = objReader.ReadToEnd().Trim();
}
objReader.Close();
objReader.Close(); result.Code = "";
}
else
{
result.Message = ex.Message;
result.Code = ((int)(res.StatusCode)).ToString();
}
return result;
}
throw new HttpPostException(ex);
}
catch (Exception ex)
{
result.Code = "";
result.Message = ex.Message;
throw new HttpPostException(ex);
}
}
return result;
}
}

HttpWebRequest

C#request 请求响应的更多相关文章

  1. django 获取request请求对象及response响应对象中的各种属性值

    django request对象和HttpResponse对象 HttpRequest对象(除非特殊说明,所有属性都是只读,session属性是个例外) HttpRequest.scheme 请求方案 ...

  2. JSP内置对象——request 及其响应get和post请求的实例

    request对象客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例.request对象具有请求域,即完成客户端的 ...

  3. jsp当做第二个servlet request的生命周期 请求 响应 不管中间经历多少个servlet 只要最后一个serlvt执行后 则生命周期结束 request的域消失

    jsp当做第二个servlet  request的生命周期   请求 响应  不管中间经历多少个servlet 只要最后一个serlvt执行后 则生命周期结束  request的域消失

  4. Django视图函数之request请求与response响应对象

    官方文档: https://docs.djangoproject.com/en/1.11/ref/request-response/ 视图中的request请求对象: 当请求页面时,Django创建一 ...

  5. request和response——请求响应对象

    请求对象--request 获取get请求的值 一次请求,返回一个响应. 地址栏:http://127.0.0.1:8000/day3/get_request?lan=python 问号:代表请求参数 ...

  6. HTTP协议请求响应过程和HTTPS工作原理

    HTTP协议 HTTP协议主要应用是在服务器和客户端之间,客户端接受超文本. 服务器按照一定规则,发送到客户端(一般是浏览器)的传送通信协议.与之类似的还有文件传送协议(file transfer p ...

  7. 基于asp.net MVC 的服务器和客户端的交互(三)之客户端请求响应

    一.分析 WEB API 中HTTP 请求方式的四个主要方法 (GET, PUT, POST, DELETE), 按照下列方式映射为 CURD 操作: GET 用于获取 URI 资源的进行展示,GET ...

  8. Charles_N:HTTP请求响应监听工具

    Charles:HTTP请求响应监听工具使用说明.doc   1.    介绍 Charles是一个HTTP代理服务器,HTTP监视器,反转代理服务器.它允许一个开发者查看所有连接互联网的HTTP通信 ...

  9. Servlet的学习之Request请求对象(3)

    本篇接上一篇,将Servlet中的HttpServletRequest对象获取RequestDispatcher对象后能进行的[转发]forward功能和[包含]include功能介绍完. 首先来看R ...

随机推荐

  1. 使用Spring进行统一日志管理 + 统一异常管理

    http://blog.csdn.net/king87130/article/details/8011843原文地址 统一日志异常实现类: 1 package com.pilelot.web.util ...

  2. Configure the max limit for concurrent TCP connections(转)

    To keep the TCP/IP stack from taking all resources on the computer, there are different parameters t ...

  3. Pgpool烂泥扶不上墙

    写这篇文章,是想好心地给打算使用Pgpool的人提个醒: Pgpool 真的不适合在企业范围使用. 我的主要理由是: 设计陈旧: 一旦后台任何节点Down掉,都会引发failover,它会杀掉所有子进 ...

  4. Bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍 中位数,数学

    1696: [Usaco2007 Feb]Building A New Barn新牛舍 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 394  Solve ...

  5. iPhone之为UIView设置阴影(CALayer的shadowColor,shadowOffset,shadowOpacity,shadowRadius,shadowPath属性)

    效果图: 以下代码实现: 第一个图片的代码 //加阴影--任海丽编辑 _imageView.layer.shadowColor = [UIColor blackColor].CGColor;//sha ...

  6. jQuery 源码分析和使用心得 - core.js

    core是jQuery的核心内容, 包含了最基础的方法, 比如我们常用的 $(selector, context), 用于遍历操作的 each, map, eq, first 识别变量类型的 isAr ...

  7. Redis数据类型--string

    在Redis中支持丰富的数据类型的存储系统,包括:字符串(string),散列(hashes),列表(lists),集合(sets),有序集合(sorted sets),与范围查询,bitmaps,h ...

  8. RN启动报错,环境相关问题

    启动RN的时候刚开始报错: The request was denied by service delegate (SBMainWorkspace) for reason: Security (&qu ...

  9. Cookie、Session详解

    讲解的很全面 https://www.cnblogs.com/andy-zhou/p/5360107.html

  10. EXTJS 下载

    Extjs各版本的下载链接 Extjs的版本繁多,本文收集了Extjs各个版本的下载链接,包括官网和非官网的,以及各种汉化版api,欢迎大家下载分享. Extjs最新版下载链接:http://www. ...