|
楼主 |
发表于 2013-8-31 08:30:10
|
显示全部楼层
帮看看第一段代码还有哪里写错了,提交之后,服务器端接收不到$_FILES数组,第二段代码就可以接收得到$_FILES数组,我就是想把第二段代码改用HttpHelper来Post
[code=csharp]public static void HttpUploadFile(string url, string poststr, string fileformname, string filepath, string fileType, string cookie, string refre)
{
String fileToUpload = filepath;
String uploadUrl = url;
String fileFormName = fileformname;
String contenttype = fileType;
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
string ContentTypeStr = "multipart/form-data; boundary=" + boundary;
StringBuilder sb = new StringBuilder();
foreach (string c in poststr.Split('&'))
{
string[] item = c.Split('=');
if (item.Length != 2)
{
break;
}
string name = item[0];
string value = item[1];
sb.Append("–" + boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"" + name + "\"");
sb.Append("\r\n\r\n");
sb.Append(value);
sb.Append("\r\n");
}
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append(fileFormName);
sb.Append("\"; filename=\"");
sb.Append(Path.GetFileName(fileToUpload));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append(contenttype);
sb.Append("\r\n");
sb.Append("\r\n");
string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
//FileStream fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read);
byte[] fileStream = File.ReadAllBytes(fileToUpload);
long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
byte[] PostData = new byte[length];
postHeaderBytes.CopyTo(PostData, 0);
fileStream.CopyTo(PostData, postHeaderBytes.Length);
boundaryBytes.CopyTo(PostData, postHeaderBytes.Length + fileStream.Length);
DotNet.Utilities.HttpHelper Http2 = new DotNet.Utilities.HttpHelper();
DotNet.Utilities.HttpItem Hitem2 = new DotNet.Utilities.HttpItem()
{
Method = "Post",
ContentType = ContentTypeStr,
URL = url,
PostdataByte = PostData,
};
SoftConfig.WriteLine(Http2.GetHtml(Hitem2).Html);
}[/code]
第二段代码:
[code=csharp]
/// <summary>
/// 上传图片文件
/// </summary>
/// <param name="url">提交的地址</param>
/// <param name="poststr">发送的文本串 比如:user=eking&pass=123456 </param>
/// <param name="fileformname">文本域的名称 比如:name="file",那么fileformname=file </param>
/// <param name="filepath">上传的文件路径 比如: c:\12.jpg </param>
/// <param name="fileType">上传的文件类型 比如:image/jpeg</param>
/// <param name="cookie">cookie数据</param>
/// <param name="refre">头部的跳转地址</param>
/// <returns></returns>
public string HttpUploadFile(string url, string poststr, string fileformname, string filepath, string fileType, string cookie, string refre)
{
String fileToUpload = filepath;
String uploadUrl = url;
String fileFormName = fileformname;
String contenttype = fileType;
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uploadUrl);
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "POST";
StringBuilder sb = new StringBuilder();
foreach (string c in poststr.Split('&'))
{
string[] item = c.Split('=');
if (item.Length != 2)
{
break;
}
string name = item[0];
string value = item[1];
sb.Append("–" + boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"" + name + "\"");
sb.Append("\r\n\r\n");
sb.Append(value);
sb.Append("\r\n");
}
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append(fileFormName);
sb.Append("\"; filename=\"");
sb.Append(Path.GetFileName(fileToUpload));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append(contenttype);
sb.Append("\r\n");
sb.Append("\r\n");
string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
FileStream fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read);
long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
webrequest.ContentLength = length;
Stream requestStream = webrequest.GetRequestStream();
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
byte[] buffer = new Byte[(int)fileStream.Length];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
WebResponse responce = webrequest.GetResponse();
requestStream.Close();
using (Stream s = responce.GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
{
return sr.ReadToEnd();
}
}
}
[/code]
|
|