功能是 使用.net模拟浏览器进行文件传输的public void SendFile(string fileName, Uri uri, CredentialCache credentialCache)
{
CookieContainer cookies = new CookieContainer();
// cast the WebRequest to a HttpWebRequest since we're using HTTPS
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Credentials = credentialCache;
httpWebRequest.CookieContainer = cookies;
WebResponse webResponse = httpWebRequest.GetResponse();HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest2.Credentials = credentialCache;
httpWebRequest2.CookieContainer = cookies;
httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest2.Method = "POST";// Build up the post message header
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"file\"; filename=\"");
sb.Append(Path.GetFileName(fileName));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: application/octet-stream");
sb.Append("\r\n");
sb.Append("\r\n");string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);// Build the trailing boundary string as a byte array
// ensuring the boundary appears on a line by itself
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");Stream requestStream = httpWebRequest2.GetRequestStream();// Write out our post header
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);// Write out the file contents
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
int bytesRead = 0;
while ( (bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0 )
requestStream.Write(buffer, 0, bytesRead);
// Write out the trailing boundary
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);WebResponse webResponse2 = httpWebRequest2.GetResponse();
}
{
CookieContainer cookies = new CookieContainer();
// cast the WebRequest to a HttpWebRequest since we're using HTTPS
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Credentials = credentialCache;
httpWebRequest.CookieContainer = cookies;
WebResponse webResponse = httpWebRequest.GetResponse();HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest2.Credentials = credentialCache;
httpWebRequest2.CookieContainer = cookies;
httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest2.Method = "POST";// Build up the post message header
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"file\"; filename=\"");
sb.Append(Path.GetFileName(fileName));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: application/octet-stream");
sb.Append("\r\n");
sb.Append("\r\n");string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);// Build the trailing boundary string as a byte array
// ensuring the boundary appears on a line by itself
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");Stream requestStream = httpWebRequest2.GetRequestStream();// Write out our post header
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);// Write out the file contents
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
int bytesRead = 0;
while ( (bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0 )
requestStream.Write(buffer, 0, bytesRead);
// Write out the trailing boundary
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);WebResponse webResponse2 = httpWebRequest2.GetResponse();
}
public void SendFile(string fileName, Uri uri, CredentialCache credentialCache)
{
CookieContainer cookies = new CookieContainer();
// 将WebRequest转换为HttpWebRequest 既然我们用的是 HTTPS
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Credentials = credentialCache;
httpWebRequest.CookieContainer = cookies;
WebResponse webResponse = httpWebRequest.GetResponse(); HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest2.Credentials = credentialCache;
httpWebRequest2.CookieContainer = cookies;
httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest2.Method = "POST"; // 生成发送信息的头
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"file\"; filename=\"");
sb.Append(Path.GetFileName(fileName));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: application/octet-stream");
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"); Stream requestStream = httpWebRequest2.GetRequestStream(); // 写入我们的发送头
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); // 写入文件内容
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (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 webResponse2 = httpWebRequest2.GetResponse();
}
{
CookieContainer cookies = new CookieContainer();
// 将WebRequest转换为HttpWebRequest 既然我们用的是 HTTPS
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Credentials = credentialCache;
httpWebRequest.CookieContainer = cookies;
WebResponse webResponse = httpWebRequest.GetResponse(); HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest2.Credentials = credentialCache;
httpWebRequest2.CookieContainer = cookies;
httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest2.Method = "POST"; // 生成发送信息的头
StringBuilder sb = new StringBuilder();
sb.Append("--");//加入到缓冲中去
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"file\"; filename=\"");
sb.Append(Path.GetFileName(fileName));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: application/octet-stream");
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"); Stream requestStream = httpWebRequest2.GetRequestStream();//获的请求对像的流 // 写入我们的发送头
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); // 写入文件内容
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (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 webResponse2 = httpWebRequest2.GetResponse();//回应
}
[code]
POST /upload_file/UploadFile HTTP/1.1Accept: text/plain, */*Accept-Language: zh-cnHost: 192.168.29.65:80Content-Type:multipart/form-data;boundary=---------------------------7d33a816d302b6
User-Agent: Mozilla/4.0 (compatible; OpenOffice.org)Content-Length: 424
Connection: Keep-Alive -----------------------------7d33a816d302b6Content-Disposition: form-data; name="userfile1"; filename="E:\s"Content-Type: application/octet-stream abbXXXccc-----------------------------7d33a816d302b6Content-Disposition: form-data; name="text1" foo-----------------------------7d33a816d302b6Content-Disposition: form-data; name="password1" bar-----------------------------7d33a816d302b6-- 此内容必须一字不差,包括最后的回车。
注意:Content-Length: 424 这里的424是红色内容的总长度(包括最后的回车)
注意这一行:
Content-Type: multipart/form-data; boundary=---------------------------7d33a816d302b6
根据 rfc1867, multipart/form-data是必须的.
---------------------------7d33a816d302b6 是分隔符,分隔多个文件、表单项。其中33a816d302b6 是即时生成的一个数字,用以确保整个分隔符不会在文件或表单项的内容中出现。前面的 ---------------------------7d 是 IE 特有的标志。 Mozila 为---------------------------71
[/code]
实际你的代码就是在构造这个协议包的实现,如果还不明白,有空看看 rfc1867 协议的内容。
POST /upload_file/UploadFile HTTP/1.1Accept: text/plain, */*Accept-Language: zh-cnHost: 192.168.29.65:80Content-Type:multipart/form-data;boundary=---------------------------7d33a816d302b6
User-Agent: Mozilla/4.0 (compatible; OpenOffice.org)Content-Length: 424
Connection: Keep-Alive -----------------------------7d33a816d302b6Content-Disposition: form-data; name="userfile1"; filename="E:\s"Content-Type: application/octet-stream abbXXXccc-----------------------------7d33a816d302b6Content-Disposition: form-data; name="text1" foo-----------------------------7d33a816d302b6Content-Disposition: form-data; name="password1" bar-----------------------------7d33a816d302b6-- 此内容必须一字不差,包括最后的回车。
注意:Content-Length: 424 这里的424是红色内容的总长度(包括最后的回车)
注意这一行:
Content-Type: multipart/form-data; boundary=---------------------------7d33a816d302b6
根据 rfc1867, multipart/form-data是必须的.
---------------------------7d33a816d302b6 是分隔符,分隔多个文件、表单项。其中33a816d302b6 是即时生成的一个数字,用以确保整个分隔符不会在文件或表单项的内容中出现。前面的 ---------------------------7d 是 IE 特有的标志。 Mozila 为---------------------------71
[/align]