SVN r538
SVN-Revision: r538
This commit is contained in:
@@ -0,0 +1,144 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Lskj.Util
|
||||||
|
{
|
||||||
|
public static class HttpHelper
|
||||||
|
{
|
||||||
|
public enum Encode
|
||||||
|
{
|
||||||
|
Default = 0,
|
||||||
|
UTF8 = 1
|
||||||
|
}
|
||||||
|
private static string ContentType = string.Empty;// "application/x-www-form-urlencoded";
|
||||||
|
private static string Accept = string.Empty;//"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
|
||||||
|
private static string UserAgent = string.Empty;//"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14";
|
||||||
|
private static Encode EncodeMode = Encode.Default;
|
||||||
|
public static void Setting(string contentType, string accept, string userAgent, Encode encode = Encode.Default)
|
||||||
|
{
|
||||||
|
ContentType = contentType;
|
||||||
|
Accept = accept;
|
||||||
|
UserAgent = userAgent;
|
||||||
|
EncodeMode = encode;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// post数据到指定的网址,获取cookie数据,和返回页
|
||||||
|
/// </summary>
|
||||||
|
public static HttpWebResponse Post(string url, string postData, Dictionary<string, string> pmsDic, Dictionary<string, string> headerDic, out string result)
|
||||||
|
{
|
||||||
|
HttpWebResponse httpWebResponse = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
HttpWebRequest httpWebRequest;
|
||||||
|
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
|
||||||
|
|
||||||
|
httpWebRequest.ContentType = ContentType;
|
||||||
|
httpWebRequest.Accept = Accept;
|
||||||
|
httpWebRequest.UserAgent = UserAgent;
|
||||||
|
httpWebRequest.Method = "POST";
|
||||||
|
byte[] byteRequest = null;
|
||||||
|
if (headerDic != null)
|
||||||
|
{
|
||||||
|
foreach (var item in headerDic)
|
||||||
|
{
|
||||||
|
if (item.Key.Equals("Content-Type"))
|
||||||
|
{
|
||||||
|
httpWebRequest.ContentType = item.Value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
httpWebRequest.Headers.Add(item.Key, item.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!string.IsNullOrEmpty(postData))
|
||||||
|
{
|
||||||
|
switch (EncodeMode)
|
||||||
|
{
|
||||||
|
case Encode.Default:
|
||||||
|
byteRequest = Encoding.Default.GetBytes(postData);
|
||||||
|
break;
|
||||||
|
case Encode.UTF8:
|
||||||
|
byteRequest = Encoding.UTF8.GetBytes(postData);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (pmsDic != null)
|
||||||
|
{
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
int i = 0;
|
||||||
|
foreach (var item in pmsDic)
|
||||||
|
{
|
||||||
|
if (i > 0)
|
||||||
|
builder.Append("&");
|
||||||
|
builder.AppendFormat("{0}={1}", item.Key, item.Value);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
switch (EncodeMode)
|
||||||
|
{
|
||||||
|
case Encode.Default:
|
||||||
|
byteRequest = Encoding.Default.GetBytes(builder.ToString());
|
||||||
|
break;
|
||||||
|
case Encode.UTF8:
|
||||||
|
byteRequest = Encoding.UTF8.GetBytes(builder.ToString());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
httpWebRequest.ContentLength = byteRequest.Length;
|
||||||
|
Stream stream = httpWebRequest.GetRequestStream();
|
||||||
|
stream.Write(byteRequest, 0, byteRequest.Length);
|
||||||
|
stream.Close();
|
||||||
|
|
||||||
|
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
|
||||||
|
Stream responseStream = httpWebResponse.GetResponseStream();
|
||||||
|
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
|
||||||
|
result = streamReader.ReadToEnd();
|
||||||
|
streamReader.Close();
|
||||||
|
responseStream.Close();
|
||||||
|
|
||||||
|
return httpWebResponse;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
result = ex.Message;
|
||||||
|
return httpWebResponse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Get到指定的网址,获取cookie数据,和返回页
|
||||||
|
/// </summary>
|
||||||
|
public static HttpWebResponse Get(string url, string postData, Dictionary<string, string> pmsDic, Dictionary<string, string> headerDic, out string result)
|
||||||
|
{
|
||||||
|
HttpWebResponse httpWebResponse = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
StringBuilder builder = new StringBuilder(url);
|
||||||
|
if (pmsDic != null && pmsDic.Count > 0)
|
||||||
|
{
|
||||||
|
builder.Append("?");
|
||||||
|
int i = 0;
|
||||||
|
foreach (var item in pmsDic)
|
||||||
|
{
|
||||||
|
if (i > 0)
|
||||||
|
builder.Append("&");
|
||||||
|
builder.AppendFormat("{0}={1}", item.Key, item.Value);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
HttpWebRequest httpWebRequest;
|
||||||
|
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(builder.ToString());
|
||||||
|
httpWebRequest.ContentType = ContentType;
|
||||||
|
//httpWebRequest.Referer = url;
|
||||||
|
httpWebRequest.Accept = Accept;
|
||||||
|
httpWebRequest.UserAgent = UserAgent;
|
||||||
|
httpWebRequest.Method = "GET";
|
||||||
|
if (headerDic != null)
|
||||||
|
{
|
||||||
|
foreach (var item in headerDic)
|
||||||
|
{
|
||||||
|
|
||||||
@@ -122,6 +122,7 @@
|
|||||||
<Compile Include="FormHelper.cs" />
|
<Compile Include="FormHelper.cs" />
|
||||||
<Compile Include="GraphicsText.cs" />
|
<Compile Include="GraphicsText.cs" />
|
||||||
<Compile Include="HandleHelper.cs" />
|
<Compile Include="HandleHelper.cs" />
|
||||||
|
<Compile Include="HttpHelper.cs" />
|
||||||
<Compile Include="HttpUtil\GetUtil.cs" />
|
<Compile Include="HttpUtil\GetUtil.cs" />
|
||||||
<Compile Include="HttpUtil\MethodEnum.cs" />
|
<Compile Include="HttpUtil\MethodEnum.cs" />
|
||||||
<Compile Include="HttpUtil\NewEmp.cs" />
|
<Compile Include="HttpUtil\NewEmp.cs" />
|
||||||
@@ -143,6 +144,7 @@
|
|||||||
<Compile Include="SpeechHelp.cs" />
|
<Compile Include="SpeechHelp.cs" />
|
||||||
<Compile Include="TaskUtil.cs" />
|
<Compile Include="TaskUtil.cs" />
|
||||||
<Compile Include="ThreadHelper.cs" />
|
<Compile Include="ThreadHelper.cs" />
|
||||||
|
<Compile Include="UploadFileUtil.cs" />
|
||||||
<Compile Include="WinHelper.cs" />
|
<Compile Include="WinHelper.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -779,22 +779,4 @@ namespace Lskj.Util
|
|||||||
/// <param name="iPersonnelCustomerId">客户标识</param>
|
/// <param name="iPersonnelCustomerId">客户标识</param>
|
||||||
/// <param name="iPersonnelPrivateKey">私钥</param>
|
/// <param name="iPersonnelPrivateKey">私钥</param>
|
||||||
/// <returns>System.String.</returns>
|
/// <returns>System.String.</returns>
|
||||||
public static string ReplaceIPersonnel(string defaultValue, string iPersonnelAllowInformation, string iPersonnelCustomerId, string iPersonnelPrivateKey)
|
public static string ReplaceIPersonnel(string defaultValue, string iPersonnelAllowInformation
|
||||||
{
|
|
||||||
if (defaultValue.Contains(_iPersonnelAllowInformation) || defaultValue.Contains(_iPersonnelEncryptedMessage))
|
|
||||||
{
|
|
||||||
//替换许可
|
|
||||||
defaultValue = defaultValue.Replace(_iPersonnelAllowInformation, iPersonnelAllowInformation);
|
|
||||||
//替换加密的签名信息
|
|
||||||
TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
|
|
||||||
long time = (long)ts.TotalMilliseconds;//当前时间的时间戳
|
|
||||||
string EncryptedMessage = string.Format("company_id={0}&mobile_no={1}×tamp={2}", iPersonnelCustomerId, ERPInfo.Instance.UserLinkPhone, time);
|
|
||||||
string value = Lskj.Util.EncryptByPrivateKey.encryptByPrivateKey(EncryptedMessage, iPersonnelPrivateKey);
|
|
||||||
defaultValue = defaultValue.Replace(_iPersonnelEncryptedMessage, value);
|
|
||||||
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
using Lskj.Model;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text;
|
||||||
|
using System.Web;
|
||||||
|
|
||||||
|
namespace Lskj.Util
|
||||||
|
{
|
||||||
|
public static class UploadFileUtil
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 登录获取token
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="oaUrl"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string Login(string oaUrl)
|
||||||
|
{
|
||||||
|
string token = "";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string loginUrl = $"{oaUrl}/Api/SysUserAjaxApi.ashx";
|
||||||
|
HttpHelper.Setting("application/x-www-form-urlencoded", null, null, HttpHelper.Encode.UTF8);
|
||||||
|
Dictionary<string, string> loginPmsDic = new Dictionary<string, string>();
|
||||||
|
loginPmsDic.Add("method", "Login");
|
||||||
|
loginPmsDic.Add("username", HttpUtility.UrlEncode(ERPInfo.Instance.UserName));
|
||||||
|
loginPmsDic.Add("password", HttpUtility.UrlEncode(ERPInfo.Instance.InPassWord));
|
||||||
|
HttpWebResponse loginResponse = HttpHelper.Post(loginUrl, "", loginPmsDic, null, out string loginResult);
|
||||||
|
if (loginResponse != null && !string.IsNullOrEmpty(loginResult))
|
||||||
|
{
|
||||||
|
JObject jObject = JsonConvert.DeserializeObject<JObject>(loginResult);
|
||||||
|
if (jObject.ContainsKey("success"))
|
||||||
|
{
|
||||||
|
if ((jObject["success"] + "").Equals("True"))
|
||||||
|
{
|
||||||
|
if (jObject.ContainsKey("token"))
|
||||||
|
{
|
||||||
|
token = jObject["token"] + "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 上传文件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <param name="url"></param>
|
||||||
|
/// <param name="fileStream"></param>
|
||||||
|
/// <param name="fileId"></param>
|
||||||
|
/// <param name="returnMsg"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool UploadFile(string token, string url, string fileStream, out string fileId, out string returnMsg)
|
||||||
|
{
|
||||||
|
bool isUpload = false;
|
||||||
|
fileId = "-1";
|
||||||
|
returnMsg = "";
|
||||||
|
if (!string.IsNullOrEmpty(token))
|
||||||
|
{
|
||||||
|
HttpHelper.Setting("application/x-www-form-urlencoded", null, null);
|
||||||
|
string result = "";
|
||||||
|
Dictionary<string, string> headerDic = new Dictionary<string, string>();
|
||||||
|
headerDic.Add("Authorization", $"Bearer {token}");
|
||||||
|
HttpWebResponse webResponse = HttpHelper.Post(url, fileStream, null, headerDic, out result);
|
||||||
|
if (webResponse != null && !string.IsNullOrEmpty(result))
|
||||||
|
{
|
||||||
|
JObject jsonObject = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(result);
|
||||||
|
string isSuccess = jsonObject["success"] + "";
|
||||||
|
if (isSuccess.Equals("True", StringComparison.CurrentCultureIgnoreCase))
|
||||||
|
{
|
||||||
|
if (jsonObject.ContainsKey("other"))
|
||||||
|
{
|
||||||
|
fileId = jsonObject["other"] + "";
|
||||||
|
}
|
||||||
|
isUpload = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (jsonObject.ContainsKey("msg"))
|
||||||
|
{
|
||||||
|
returnMsg = jsonObject["msg"] + "";
|
||||||
|
}
|
||||||
|
isUpload = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
returnMsg = "上传请求失败";
|
||||||
|
isUpload = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
returnMsg
|
||||||
Reference in New Issue
Block a user