/// /// /// 指定数据._c# http post">
赞
踩
- /// <summary>
- /// post发送字符串数据-可指定数据类型,可设置timeout
- /// </summary>
- /// <param name="Url"></param>
- /// <param name="SendData"></param>
- /// <param name="SendData"></param>
- /// <param name="ContentType">指定数据类型(text/html, text/plain, text/css, text/javascript, image/jpeg, image/png, image/gif,application/x-www-form-urlencoded, multipart/form-data, application/json, application/xml)</param>
- /// <param name="timeout">指定超时时间</param>
- /// <param name="state">返回操作状态0:正常;远程服务器报错;数据提交超时</param>
- /// <returns></returns>
- public static string SendRequestPostData(string Url, string SendData, string ContentType, int timeout, Dictionary<string, string> headers
- , ref string Status, ref int StatusCode, string Tag, string LogName)
- {
- Status = "OK";
- System.GC.Collect();//垃圾回收,回收没有正常关闭的http连接
- string strURL = Url;
- //设置最大连接数
- ServicePointManager.DefaultConnectionLimit = 200;
- //设置https验证方式
- if (Url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
- {
- ServicePointManager.ServerCertificateValidationCallback =
- new RemoteCertificateValidationCallback(CheckValidationResult);
- }
- //创建一个HTTP请求
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
- //Post请求方式
- request.Method = "POST";
- if (timeout != 0)
- {
- request.Timeout = timeout;
- }
- CookieContainer cookieContainer = new CookieContainer();
- request.CookieContainer = cookieContainer;
- request.AllowAutoRedirect = true;
- //内容类型
- request.ContentType = ContentType == null ? "text/html" : ContentType;
- if (headers != null)
- {
- foreach (string key in headers.Keys)
- {
- request.Headers.Add(key, headers[key]);
- }
- }
-
- //设置参数,并进行URL编码
-
- string paraUrlCoded = SendData;//System.Web.HttpUtility.UrlEncode(jsonParas);
-
- byte[] payload;
- //将Json字符串转化为字节
- payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
- //设置请求的ContentLength
- request.ContentLength = payload.Length;
- //发送请求,获得请求流
-
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
- Stream writer;
- try
- {
- writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
- }
- catch (Exception ex)
- {
- Status = ex.Message;
- T9.Util.LogUtil.WriteLog("SendRequestPostData方法出错[0]" + (Tag != null ? "[" + Tag + "]" : "") + ":" + ex.Message + "\r\n" + ex.StackTrace, LogName);
- writer = null;
- request.Abort();
- return null;
- }
- //将请求参数写入流
- writer.Write(payload, 0, payload.Length);
- writer.Close();//关闭请求流
- HttpWebResponse response = null;
- string postContent = null;
- try
- {
- //获得响应流
- response = (HttpWebResponse)request.GetResponse();
- }
- catch (WebException ex)
- {
- Status = ex.Status.ToString();
- T9.Util.LogUtil.WriteLog("SendRequestPostData方法出错[1]" + (Tag != null ? "[" + Tag + "]" : "") + ":" + ex.Message + "\r\n" + ex.StackTrace, LogName);
- response = ex.Response as HttpWebResponse;
- }
- finally
- {
- if (response == null)
- {
- postContent = null;
- }
- else
- {
- StatusCode = (int)response.StatusCode;
- Stream s = response.GetResponseStream();
- StreamReader sRead = new StreamReader(s);
- postContent = sRead.ReadToEnd();
- request.Abort();
- response.Close();
- sRead.Close();
- }
- }
- return postContent;//返回Json数据
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。