89 lines
2.9 KiB
C#
89 lines
2.9 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
|
|
namespace WX.CRM.Common
|
|
{
|
|
public class CMSApiHelper
|
|
{
|
|
private string url = "http://localhost:30139/";
|
|
|
|
public CMSApiResult<PushDataRes> PushImportData(Dictionary<string, object> param)
|
|
{
|
|
var pushUrl = $"{url}/Import/ImportData.html";
|
|
var req = Post(pushUrl, param, new Dictionary<string, string>());
|
|
|
|
|
|
return JsonConvert.DeserializeObject<CMSApiResult<PushDataRes>>(req);
|
|
}
|
|
public CMSApiResult<string> SubmitImport(Dictionary<string, object> param)
|
|
{
|
|
var pushUrl = $"{url}/Import/ImportSubmit.html";
|
|
var req = Post(pushUrl, param, new Dictionary<string, string>());
|
|
|
|
|
|
return JsonConvert.DeserializeObject<CMSApiResult<string>>(req);
|
|
}
|
|
/// <summary>
|
|
/// 内置请求
|
|
/// </summary>
|
|
/// <param name=""></param>
|
|
/// <returns></returns>
|
|
private string Post(string url, Dictionary<string, object> param, Dictionary<string, string> header)
|
|
{
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
//请求设置
|
|
request.Method = "POST";
|
|
request.ContentType = "application/json;charset=UTF-8";
|
|
request.Timeout = 2000;
|
|
//拼接请求头
|
|
foreach (var item in header)
|
|
{
|
|
request.Headers.Add(item.Key, item.Value);
|
|
}
|
|
//拼接参数
|
|
if (param.Any())
|
|
{
|
|
var pStr = JsonConvert.SerializeObject(param);
|
|
byte[] data = Encoding.UTF8.GetBytes(pStr);
|
|
using (Stream stream = request.GetRequestStream())
|
|
{
|
|
stream.Write(data, 0, data.Length);
|
|
}
|
|
}
|
|
try
|
|
{
|
|
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
|
{
|
|
//解析请求结果
|
|
using (Stream myResponseStream = response.GetResponseStream())
|
|
{
|
|
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
|
|
string retString = myStreamReader.ReadToEnd();
|
|
myStreamReader.Close();
|
|
return retString;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex.Message;
|
|
}
|
|
}
|
|
}
|
|
public class CMSApiResult<T>
|
|
{
|
|
public string msg { get; set; }
|
|
public int code { get; set; }
|
|
public T data { get; set; }
|
|
}
|
|
public class PushDataRes
|
|
{
|
|
public string pushCode { get; set; }
|
|
}
|
|
}
|