1.普通HttpPost方法
private string HttpPost(string postUrl, string postContent,string postContentType)
{
string result = "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(postUrl);
req.Method = "POST";
req.ContentType = postContentType;
//region 添加Post 参数
byte[] data = Encoding.UTF8.GetBytes(postContent);
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
//#endregion
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
reader.Close();
}
return result;
}
2.HttpPost multipart/form-data 方法 网上找的能用;
private string HttpPostMulti(string url, byte[] body, string fileName, Dictionary<string, string> headers = null, string contentType = null, int timeout = 60, Encoding encoding = null)
{
string result = string.Empty;
HttpWebRequest request = null;
HttpWebResponse response = null;
Stream requestStream = null;
Stream responseStream = null;
try
{
request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Timeout = -1;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
//对发送的数据不使用缓存【重要、关键】
request.AllowWriteStreamBuffering = false;
request.SendChunked = true;//支持分块上传
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
//请求头部信息
StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"content\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
//request.AddRange(body.Length);
requestStream = request.GetRequestStream();
requestStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
requestStream.Write(body, 0, body.Length);
requestStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
response = (HttpWebResponse)request.GetResponse();
responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
result = streamReader.ReadToEnd();//返回信息
streamReader.Close();
streamReader.Dispose();
requestStream.Dispose();
responseStream.Close();
responseStream.Dispose();
}
catch (Exception ex)
{
return "";
}
finally
{
//Dispose(request, response, requestStream, responseStream);
}
return result;
}
3.文件转字节数组以上传
private byte[] fileTobyte(string filePath)
{
byte[] buffer;
using (FileStream fs = File.OpenRead(filePath))
{
buffer = new byte[fs.Length];
byte[] b = new byte[1024];
//UTF8Encoding temp = new UTF8Encoding(true);
int i = 0;
while (fs.Read(b, 0, b.Length) > 0)
{
for (int j = 0; j < 1024; j++)
{
if (i * 1024 + j < buffer.Length)
{
buffer[i * 1024 + j] = b[j];
}
}
i++;
}
fs.Close();
fs.Dispose();
}
return buffer;
}
4.MD5加密
private string StandardMD5(String s)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
bytes = md5.ComputeHash(bytes);
md5.Clear();
string ret = "";
for (int i = 0; i < bytes.Length; i++)
{
ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
}
return ret.PadLeft(32, '0');
}
5.HMACSHA1加密
private string HMACSHA1Text(string text, string key)
{
//HMACSHA1加密
HMACSHA1 hmacsha1 = new HMACSHA1();
hmacsha1.Key = System.Text.Encoding.UTF8.GetBytes(key);
byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(text);
byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
return Convert.ToBase64String(hashBytes);
}
6.获得当前十位时间戳
TimeSpan timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
ts = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
7.JObject JArray 混合使用 适用数据结构为 "{\"other\": 233,\"data\": [{\"a\": 33, \"b\": 2}]}" 取data中a的值
string translateResult = "{\"other\": 233,\"data\": [{\"a\": 33, \"b\": 2}]}";
JObject getTranslateResult = (JObject)JsonConvert.DeserializeObject(translateResult);
string translateResultData = getTranslateResult["data"].ToString();
JArray array = JArray.Parse(translateResultData);
finalResult = array[0]["a"].ToString();
C#常用方法函数
最新推荐文章于 2023-06-29 14:04:08 发布