1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Security;
- using System.Security.Cryptography.X509Certificates;
- using System.Text;
- using System.Threading.Tasks;
- namespace NX_CommonClassLibrary
- {
- public static class HttpRequestHelper
- {
- /// <summary>
- /// 票据
- /// </summary>
- public static string _Ticket { get; set; }
- private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
- {
- return true;
- }
- public static string POST(string url, string pramstr)
- {
- HttpWebRequest request = null;
- if (url.Contains("https://"))
- {
- ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateValidate);
- request = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
- }
- else
- {
- request = (HttpWebRequest)WebRequest.Create(url);
- }
- request.Method = "POST";
- request.ContentType = "application/json; charset=utf-8";
- if (!string.IsNullOrWhiteSpace(_Ticket))
- {
- request.Headers.Add("Authorization", "Bearer " + _Ticket);
- }
- var jsonbyte = Encoding.UTF8.GetBytes(pramstr);
- using (var postStream = request.GetRequestStream())
- {
- postStream.Write(jsonbyte, 0, jsonbyte.Length);
- }
- HttpWebResponse res;
- StreamReader sr;
- string content;
- try
- {
- res = (HttpWebResponse)request.GetResponse();
- sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
- content = sr.ReadToEnd();
- }
- catch (WebException ex)
- {
- content = ex.Message;
- }
- return content;
- }
- }
- }
|