init lserp cs 5.0
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,218 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
using System.IO;
|
||||
|
||||
namespace Lskj.DevFx.Network
|
||||
{
|
||||
/// <summary>
|
||||
/// HTTP请求类
|
||||
/// </summary>
|
||||
public class HttpRequest
|
||||
{
|
||||
private int _timeout = 100000;
|
||||
|
||||
/// <summary>
|
||||
/// 请求与响应的超时时间
|
||||
/// </summary>
|
||||
public int Timeout
|
||||
{
|
||||
get { return this._timeout; }
|
||||
set { this._timeout = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行HTTP POST请求。
|
||||
/// </summary>
|
||||
/// <param name="url">请求地址</param>
|
||||
/// <param name="parameters">请求参数</param>
|
||||
/// <returns>HTTP响应</returns>
|
||||
public string DoPost(string url, IDictionary<string, string> parameters)
|
||||
{
|
||||
HttpWebRequest req = GetWebRequest(url, "POST");
|
||||
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
|
||||
|
||||
byte[] postData = Encoding.UTF8.GetBytes(BuildQuery(parameters));
|
||||
Stream reqStream = req.GetRequestStream();
|
||||
reqStream.Write(postData, 0, postData.Length);
|
||||
reqStream.Close();
|
||||
|
||||
HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
|
||||
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
|
||||
return GetResponseAsString(rsp, encoding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行HTTP GET请求。
|
||||
/// </summary>
|
||||
/// <param name="url">请求地址</param>
|
||||
/// <param name="parameters">请求参数</param>
|
||||
/// <returns>HTTP响应</returns>
|
||||
public string DoGet(string url, IDictionary<string, string> parameters)
|
||||
{
|
||||
if (parameters != null && parameters.Count > 0)
|
||||
{
|
||||
if (url.Contains("?"))
|
||||
{
|
||||
url = url + "&" + BuildQuery(parameters);
|
||||
}
|
||||
else
|
||||
{
|
||||
url = url + "?" + BuildQuery(parameters);
|
||||
}
|
||||
}
|
||||
|
||||
HttpWebRequest req = GetWebRequest(url, "GET");
|
||||
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
|
||||
|
||||
HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
|
||||
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
|
||||
return GetResponseAsString(rsp, encoding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行带文件上传的HTTP POST请求。
|
||||
/// </summary>
|
||||
/// <param name="url">请求地址</param>
|
||||
/// <param name="textParams">请求文本参数</param>
|
||||
/// <param name="fileParams">请求文件参数</param>
|
||||
/// <returns>HTTP响应</returns>
|
||||
public string DoPost(string url, IDictionary<string, string> textParams, IDictionary<string, UploadFileItem> fileParams)
|
||||
{
|
||||
// 如果没有文件参数,则走普通POST请求
|
||||
if (fileParams == null || fileParams.Count == 0)
|
||||
{
|
||||
return DoPost(url, textParams);
|
||||
}
|
||||
|
||||
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
|
||||
|
||||
HttpWebRequest req = GetWebRequest(url, "POST");
|
||||
req.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
|
||||
|
||||
Stream reqStream = req.GetRequestStream();
|
||||
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
|
||||
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
|
||||
|
||||
// 组装文本请求参数
|
||||
string textTemplate = "Content-Disposition:form-data;name=\"{0}\"\r\nContent-Type:text/plain\r\n\r\n{1}";
|
||||
IEnumerator<KeyValuePair<string, string>> textEnum = textParams.GetEnumerator();
|
||||
while (textEnum.MoveNext())
|
||||
{
|
||||
string textEntry = string.Format(textTemplate, textEnum.Current.Key, textEnum.Current.Value);
|
||||
byte[] itemBytes = Encoding.UTF8.GetBytes(textEntry);
|
||||
reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
|
||||
reqStream.Write(itemBytes, 0, itemBytes.Length);
|
||||
}
|
||||
|
||||
// 组装文件请求参数
|
||||
string fileTemplate = "Content-Disposition:form-data;name=\"{0}\";filename=\"{1}\"\r\nContent-Type:{2}\r\n\r\n";
|
||||
IEnumerator<KeyValuePair<string, UploadFileItem>> fileEnum = fileParams.GetEnumerator();
|
||||
while (fileEnum.MoveNext())
|
||||
{
|
||||
string key = fileEnum.Current.Key;
|
||||
UploadFileItem fileItem = fileEnum.Current.Value;
|
||||
string fileEntry = string.Format(fileTemplate, key, fileItem.GetFileName(), fileItem.GetMimeType());
|
||||
byte[] itemBytes = Encoding.UTF8.GetBytes(fileEntry);
|
||||
reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
|
||||
reqStream.Write(itemBytes, 0, itemBytes.Length);
|
||||
|
||||
byte[] fileBytes = fileItem.GetContent();
|
||||
reqStream.Write(fileBytes, 0, fileBytes.Length);
|
||||
}
|
||||
|
||||
reqStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
|
||||
reqStream.Close();
|
||||
|
||||
HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
|
||||
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
|
||||
return GetResponseAsString(rsp, encoding);
|
||||
}
|
||||
|
||||
public HttpWebRequest GetWebRequest(string url, string method)
|
||||
{
|
||||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
||||
req.ServicePoint.Expect100Continue = false;
|
||||
req.Method = method;
|
||||
req.KeepAlive = true;
|
||||
req.UserAgent = "Lskj.DevFx";
|
||||
req.Timeout = this._timeout;
|
||||
return req;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 把响应流转换为文本。
|
||||
/// </summary>
|
||||
/// <param name="rsp">响应流对象</param>
|
||||
/// <param name="encoding">编码方式</param>
|
||||
/// <returns>响应文本</returns>
|
||||
public string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
|
||||
{
|
||||
StringBuilder result = new StringBuilder();
|
||||
Stream stream = null;
|
||||
StreamReader reader = null;
|
||||
|
||||
try
|
||||
{
|
||||
// 以字符流的方式读取HTTP响应
|
||||
stream = rsp.GetResponseStream();
|
||||
reader = new StreamReader(stream, encoding);
|
||||
|
||||
// 按字符读取并写入字符串缓冲
|
||||
int ch = -1;
|
||||
while ((ch = reader.Read()) > -1)
|
||||
{
|
||||
// 过滤结束符
|
||||
char c = (char)ch;
|
||||
if (c != '\0')
|
||||
{
|
||||
result.Append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
// 释放资源
|
||||
if (reader != null) reader.Close();
|
||||
if (stream != null) stream.Close();
|
||||
if (rsp != null) rsp.Close();
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 组装普通文本请求参数。
|
||||
/// </summary>
|
||||
/// <param name="parameters">Key-Value形式请求参数字典</param>
|
||||
/// <returns>URL编码后的请求数据</returns>
|
||||
public static string BuildQuery(IDictionary<string, string> parameters)
|
||||
{
|
||||
StringBuilder postData = new StringBuilder();
|
||||
bool hasParam = false;
|
||||
|
||||
IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
|
||||
while (dem.MoveNext())
|
||||
{
|
||||
string name = dem.Current.Key;
|
||||
string value = dem.Current.Value;
|
||||
// 忽略参数名或参数值为空的参数
|
||||
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
|
||||
{
|
||||
if (hasParam)
|
||||
{
|
||||
postData.Append("&");
|
||||
}
|
||||
|
||||
postData.Append(name);
|
||||
postData.Append("=");
|
||||
postData.Append(Uri.EscapeDataString(value));
|
||||
hasParam = true;
|
||||
}
|
||||
}
|
||||
|
||||
return postData.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Lskj.DevFx.Utils;
|
||||
|
||||
namespace Lskj.DevFx.Network
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件元数据。
|
||||
/// 可以使用以下几种构造方法:
|
||||
/// 本地路径:new UploadFileItem("C:/temp.jpg");
|
||||
/// 本地文件:new UploadFileItem(new FileInfo("C:/temp.jpg"));
|
||||
/// 字节流:new UploadFileItem("abc.jpg", bytes);
|
||||
/// </summary>
|
||||
public class UploadFileItem
|
||||
{
|
||||
private string fileName;
|
||||
private string mimeType;
|
||||
private byte[] content;
|
||||
private FileInfo fileInfo;
|
||||
|
||||
/// <summary>
|
||||
/// 基于本地文件的构造器。
|
||||
/// </summary>
|
||||
/// <param name="fileInfo">本地文件</param>
|
||||
public UploadFileItem(FileInfo fileInfo)
|
||||
{
|
||||
if (fileInfo == null || !fileInfo.Exists)
|
||||
{
|
||||
throw new ArgumentException("fileInfo is null or not exists!");
|
||||
}
|
||||
this.fileInfo = fileInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 基于本地文件全路径的构造器。
|
||||
/// </summary>
|
||||
/// <param name="filePath">本地文件全路径</param>
|
||||
public UploadFileItem(string filePath)
|
||||
: this(new FileInfo(filePath))
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// 基于文件名和字节流的构造器。
|
||||
/// </summary>
|
||||
/// <param name="fileName">文件名称(服务端持久化字节流到磁盘时的文件名)</param>
|
||||
/// <param name="content">文件字节流</param>
|
||||
public UploadFileItem(string fileName, byte[] content)
|
||||
{
|
||||
if (string.IsNullOrEmpty(fileName)) throw new ArgumentNullException("fileName");
|
||||
if (content == null || content.Length == 0) throw new ArgumentNullException("content");
|
||||
|
||||
this.fileName = fileName;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 基于文件名、字节流和媒体类型的构造器。
|
||||
/// </summary>
|
||||
/// <param name="fileName">文件名(服务端持久化字节流到磁盘时的文件名)</param>
|
||||
/// <param name="content">文件字节流</param>
|
||||
/// <param name="mimeType">媒体类型</param>
|
||||
public UploadFileItem(String fileName, byte[] content, String mimeType)
|
||||
: this(fileName, content)
|
||||
{
|
||||
if (string.IsNullOrEmpty(mimeType)) throw new ArgumentNullException("mimeType");
|
||||
this.mimeType = mimeType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件名
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetFileName()
|
||||
{
|
||||
if (this.fileName == null && this.fileInfo != null && this.fileInfo.Exists)
|
||||
{
|
||||
this.fileName = this.fileInfo.FullName;
|
||||
}
|
||||
return this.fileName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件的 mime-type
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetMimeType()
|
||||
{
|
||||
if (this.mimeType == null)
|
||||
{
|
||||
this.mimeType = FileHelper.GetContentTypeForFileName(GetFileName());
|
||||
}
|
||||
return this.mimeType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件的二进制流
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public byte[] GetContent()
|
||||
{
|
||||
if (this.content == null && this.fileInfo != null && this.fileInfo.Exists)
|
||||
{
|
||||
using (Stream fileStream = this.fileInfo.OpenRead())
|
||||
{
|
||||
this.content = new byte[fileStream.Length];
|
||||
fileStream.Read(content, 0, content.Length);
|
||||
}
|
||||
}
|
||||
|
||||
return this.content;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user