123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.IO.Compression;
- using System.Net;
- using System.Net.Security;
- using System.Security.Cryptography.X509Certificates;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace SCC.Common
- {
- /// <summary>
- /// Http连接操作帮助类
- /// </summary>
- public class HttpHelper
- {
- public string PostJson(Dictionary<string, string> dic,string url)
- {
- try
- {
- string result = "";
- HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
- req.Method = "POST";
- req.ContentType = "application/x-www-form-urlencoded";
- #region 添加Post 参数
- StringBuilder builder = new StringBuilder();
- int i = 0;
- foreach (var item in dic)
- {
- if (i > 0)
- builder.Append("&");
- builder.AppendFormat("{0}={1}", item.Key, item.Value);
- i++;
- }
- byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
- req.ContentLength = data.Length;
- using (Stream reqStream = req.GetRequestStream())
- {
- reqStream.Write(data, 0, data.Length);
- reqStream.Close();
- }
- #endregion
- HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
- Stream stream = resp.GetResponseStream();
- //获取响应内容
- using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
- {
- result = reader.ReadToEnd();
- }
- return result;
- }
- catch (Exception e)
- {
- return "";
-
- }
-
- }
- }
- }
|