protected string CreateFormDateResponse(string url, Encoding encoding, IDictionary<string, string> textParams, IDictionary<string, FileinFo> fileParams)
{
try
{
HttpWebRequest request;
// 重新设置请求头
request = (HttpWebRequest)WebRequest.Create(url);
string host = url.Replace("http://", "").Replace("https://", "");
host = host.Remove(host.IndexOf('/'));
request.Host = host;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8;";
request.KeepAlive = true;
request.Referer = url;
request.ProtocolVersion = HttpVersion.Version11;
request.Method = "POST";
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
request.ContentType = "multipart/form-data;charset=gb2312;boundary=" + boundary;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; LCTE; rv:11.0) like Gecko";
request.Timeout = 5000;
request.Headers["Upgrade-Insecure-Requests"] = "1";
request.Headers["Accept-Encoding"] = "gzip, deflate";
request.Headers["Accept-Language"] = "zh-CN,zh;q=0.9";
request.Headers["Cache-Control"] = "no-cache";
request.Headers["Pragma"] = "no-cache";
// 自动追踪30X跳转
request.AllowAutoRedirect = true;
byte[] itemBoundaryBytes = encoding.GetBytes("\r\n--" + boundary + "\r\n");
byte[] endBoundaryBytes = encoding.GetBytes("\r\n--" + boundary + "--\r\n");
// 组装文本请求参数
string textTemplate = "Content-Disposition:form-data;name=\"{0}\"\r\n\r\n{1}";
IEnumerator<KeyValuePair<string, string>> textEnum = textParams.GetEnumerator();
using (System.IO.Stream reqStream = request.GetRequestStream())
{
while (textEnum.MoveNext())
{
string textEntry = string.Format(textTemplate, textEnum.Current.Key, textEnum.Current.Value);
byte[] itemBytes = encoding.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, FileinFo>> fileEnum = fileParams.GetEnumerator();
while (fileEnum.MoveNext())
{
string key = fileEnum.Current.Key;
FileinFo fileItem = fileEnum.Current.Value;
string fileEntry = string.Format(fileTemplate, key, fileItem.filename, fileItem.ContentType);
byte[] itemBytes = encoding.GetBytes(fileEntry);
reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
reqStream.Write(itemBytes, 0, itemBytes.Length);
byte[] fileBytes = fileItem.data;
reqStream.Write(fileBytes, 0, fileBytes.Length);
}
reqStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
reqStream.Close();
}
ServicePointManager.DefaultConnectionLimit = 200;
using (HttpWebResponse rsp = (HttpWebResponse)request.GetResponse())
{
try
{ // 以字符流的方式读取HTTP响应
using (Stream stream = rsp.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, encoding))
{
return reader.ReadToEnd();
}
}
}
catch (Exception ex)
{
LogInfo.Error("响应文本错误", ex);
return null;
}
finally
{
// 释放资源
if (rsp != null) rsp.Close();
}
}
}
catch (WebException e)
{
LogInfo.Error("WebException异常url=" + url + ";\r\ndatabuffer=", e);
return string.Empty;
}
catch (Exception e)
{
LogInfo.Error("异常url=" + url, e);
return string.Empty;
}
}
public class FileinFo
{
public string name { set; get; }
public string filename { set; get; }
public string ContentType { set; get; }
public byte[] data { set; get; }
}