纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

68 lines
2.8 KiB

using RestSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace NFine.Data.Extensions
{
public class HTTPHelper
{
//private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static string HttpPost(string apiName, string url, string body)
{
try
{
//log.Debug(url + Environment.NewLine + body);
Encoding encoding = Encoding.UTF8;
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.Method = "POST";
request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
request.ContentType = "application/json; charset=utf-8";
// request.ContentType = "text/html, application/xhtml+xml";
byte[] buffer = encoding.GetBytes(body);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
{
return reader.ReadToEnd();
}
}
catch (System.Net.WebException ex)
{
//log.Error(ex.ToString() + Environment.NewLine + url + Environment.NewLine + body);
throw new Exception(apiName + "调用失败," + ex.Message);
}
}
public static string RestFulGet(string jsonParam, string url)
{
try
{
//var userName = "TEST";
//var password = "12345678";
//正式地址
var userName = "ADVANCED";
var password = "SYSTEM08";
var client = new RestClient(url);
client.Authenticator = new HttpBasicAuthenticator(userName, password);
var request = new RestRequest(Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", jsonParam, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response.Content;
}
catch (Exception ex)
{
throw new Exception("货柜传输检验批接口调用失败," + ex.Message);
}
}
}
}