初始化
This commit is contained in:
123
BLV_API/Tools.cs
Normal file
123
BLV_API/Tools.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
|
||||
namespace BLV_API
|
||||
{
|
||||
public static class Tools
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 获取IP
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetClientIP(HttpRequestBase Request)
|
||||
{
|
||||
string IP = "";
|
||||
string PPP =Request.UserHostAddress;
|
||||
string III =Request.ServerVariables["REMOTE_ADDR"];
|
||||
if (string.IsNullOrEmpty(III))
|
||||
{
|
||||
IP = PPP;
|
||||
}
|
||||
else
|
||||
{
|
||||
IP = III;
|
||||
}
|
||||
return IP;
|
||||
}
|
||||
public static string GetApplicationPath()
|
||||
{
|
||||
return AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
|
||||
}
|
||||
public static string MD5Encrypt(string strEnc)
|
||||
{
|
||||
string result;
|
||||
try
|
||||
{
|
||||
MD5 md5Hasher = MD5.Create();
|
||||
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(strEnc));
|
||||
StringBuilder sBuilder = new StringBuilder();
|
||||
int num;
|
||||
for (int i = 0; i < data.Length; i = num + 1)
|
||||
{
|
||||
sBuilder.Append(data[i].ToString("x2"));
|
||||
num = i;
|
||||
}
|
||||
result = sBuilder.ToString();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// post数据接口
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="postData"></param>
|
||||
/// <param name="method">post和put,默认post</param>
|
||||
/// <returns></returns>
|
||||
public static string PostWebRequest(string url, string postData)
|
||||
{
|
||||
string result = string.Empty;
|
||||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
||||
req.Method = "POST";
|
||||
//req.Timeout = 10000;//设置请求超时时间,单位为毫秒
|
||||
req.ContentType = "application/json";
|
||||
byte[] data = Encoding.UTF8.GetBytes(postData);
|
||||
req.ContentLength = data.Length;
|
||||
using (Stream reqStream = req.GetRequestStream())
|
||||
{
|
||||
reqStream.Write(data, 0, data.Length);
|
||||
}
|
||||
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
||||
using (Stream stream = resp.GetResponseStream())
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
||||
{
|
||||
result = reader.ReadToEnd();//获取响应内容
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// post数据接口
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="headerAuthorization"></param>
|
||||
/// <param name="postData"></param>
|
||||
/// <returns></returns>
|
||||
public static string PostWebRequest(string url, string headerAuthorization, string postData)
|
||||
{
|
||||
string result = string.Empty;
|
||||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
||||
req.Method = "POST";
|
||||
req.ContentType = "application/x-www-form-urlencoded";
|
||||
req.Headers.Add("Authorization", headerAuthorization);
|
||||
byte[] data = Encoding.UTF8.GetBytes(postData);
|
||||
req.ContentLength = data.Length;
|
||||
using (Stream reqStream = req.GetRequestStream())
|
||||
{
|
||||
reqStream.Write(data, 0, data.Length);
|
||||
}
|
||||
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
||||
using (Stream stream = resp.GetResponseStream())
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
||||
{
|
||||
result = reader.ReadToEnd();//获取响应内容
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user