1 private void PostFile(string url,byte[] data,string fileName)
2 {
3
4 string path = Server.MapPath("/");
5
6 string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
7 StringBuilder sb = new StringBuilder();
8 sb.Append("--" + boundary);
9 sb.Append("\r\n");
10 sb.Append("Content-Disposition: form-data; name=\"image\"; filename=\"" + path +fileName+ "\"");
11 sb.Append("\r\n");
12 sb.Append("Content-Type: image/pjpeg");
13 sb.Append("\r\n\r\n");
14
15 string head = sb.ToString();
16
17
18
19 byte[] form_data = Encoding.UTF8.GetBytes(head);
20 //结尾
21 byte[] foot_data = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
22
23
24 string pwd = System.Configuration.ConfigurationManager.AppSettings["imgserverpwd"].ToString();
25 HttpWebRequest HRQ = (HttpWebRequest)System.Net.WebRequest.Create(url + "?filename=" + fileName + "&p=" + pwd);
26 HRQ.Method = "POST";
27 HRQ.KeepAlive = false;
28 HRQ.ContentType = "multipart/form-data";
29 HRQ.Timeout = 10 * 1000;
30 HRQ.ContentLength = form_data.Length + data.Length + foot_data.Length;
31 //HRQ.ContentLength = data.Length;
32
33
34 Stream sr = HRQ.GetRequestStream();
35 sr.Write(form_data, 0, form_data.Length);
36 sr.Write(data, 0, data.Length);
37 sr.Write(foot_data, 0, foot_data.Length);
38
39
40 HttpWebResponse RES = (HttpWebResponse)HRQ.GetResponse();
41
42
43 if (HRQ.HaveResponse)
44 {
45 Stream Rs = RES.GetResponseStream();
46 StreamReader RsRead = new StreamReader(Rs);
47 Response.Write(RsRead.ReadToEnd());
48 }
49 else
50 {
51 Response.Write("<p>" + url + ":<span style=\"color:#f00\">失败</span></p>");
52 }
53 sr.Close();
54 sr.Dispose();
55 }
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Net.Sockets.SocketException: 您的主机中的软件放弃了一个已建立的连接。
源错误:
行 96: sr.Write(form_data, 0, form_data.Length);
行 97: sr.Write(data, 0, data.Length);
行 98: sr.Write(foot_data, 0, foot_data.Length); 行 99:
行 100:
|
源文件: g:\upl\upload\index.aspx.cs 行: 98
堆栈跟踪:
[SocketException (0x2745): 您的主机中的软件放弃了一个已建立的连接。] System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) +5390724 System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size) +131 [IOException: 无法将数据写入传输连接: 您的主机中的软件放弃了一个已建立的连接。。] System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size) +294 System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size) +26 System.Net.ConnectStream.InternalWrite(Boolean async, Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) +1308 System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32 size) +105 upload_index.PostFile(String url, Byte[] data, String fileName) in g:\upl\upload\index.aspx.cs:98 upload_index.TransportFile(HttpPostedFile File) in g:\upl\upload\index.aspx.cs:49 upload_index.Page_Load(Object sender, EventArgs e) in g:\upl\upload\index.aspx.cs:24 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627 |
这个错误有很多原因引起的。比如杀毒软件。有可能是SOCKET连接已关闭。
不过你可以避免这个情况。每次在write的时候:
sr.CanWrite() 来判断一下。
另外你可以用长连接来试一下。把KeepAlive设置为true
有可能是你开了多个vs导致的,建议关闭多个,留一个,http服务器可以发布到iis再访问。我用这种办法解决了。