在WinForm中通过HTTP协议向服务器端上传文件(8) } stream1.Close(); stream1 = null; response1 = request1.GetResponse(); this.m_responseHeaders = response1.Headers; return this.ResponseAsBytes(response1); } catch (Exception exception1) { if (stream1 != null) { stream1.Close(); stream1 = null; } if ((exception1 is WebException) (exception1 is SecurityException)) { throw; } throw new WebException(SR.GetString("net_webclient"), exception1); } return buffer4;} 在这段代码里面其实最关键的就是如何模拟POST请求,通过分析代码和监视HTTP,我们可以发现模拟的POST格式如下:-----------------------8c64f47716481f0 //时间戳
Content-Disposition: form-data; name="file"; filename="a.txt" //文件名
Content-Type: application/octet-stream
//文件的内容
-----------------------8c64f47716481f0
这时候,我们只需自己编码来模拟这么一组数据就行(我们还可以好好借鉴MS的代码呢),以下就是代码(声明一下,我是借用了别人的代码)