代码1是一个简单的htm页面,代码2是我用HttpWebRequest 模拟浏览器进行文件传输的代码,在局域网内两种方法传输都正常。
但是,传输到这个地址“http://<server-ip>/OmEntrance_Rt/Entrance.aspx”代码1正常,但代码2会返回一个错误“远程服务器段返回错误:(500)内部服务器错误”,不明白是怎么回事,请高手指点,谢谢了!
代码1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="http://<server-ip>/OmEntrance_Rt/Entrance.aspx">
<input type="file" name="file" />
<input type="submit" name="Submit" value="提交" />
</form>
</body>
</html>代码2
protected void Button1_Click(object sender, System.EventArgs e)
{
string fullfilename = "d:\\huanbao\\send\\text.xml";
Uri u1 = new Uri("http://<server-ip>/OmEntrance_Rt/Entrance.aspx");
CredentialCache credentialCache1 = new CredentialCache();
this.SendFile(fullfilename, u1, credentialCache1);
} public void SendFile(string fileName, Uri uri, CredentialCache credentialCache)
{
CookieContainer cookies = new CookieContainer();
string boundary = "----------";
HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest2.Credentials = credentialCache;
httpWebRequest2.CookieContainer = cookies;
// httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest2.ContentType = "multipart/form-data";
// httpWebRequest2.ContentType = "application/x-www-form-urlencoded";
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("Content-Type: application/x-xml");
//sb.Append("Content-Type: text/xml");
sb.Append("\r\n");
sb.Append("\r\n"); string postHeader = sb.ToString();
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
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"); httpWebRequest2.ContentLength = boundaryBytes.Length + postHeaderBytes.Length + fileStream.Length; 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))];
byte[] buffer = new Byte[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);
requestStream.Close(); WebResponse webResponse2 = httpWebRequest2.GetResponse(); Stream stream = null; stream = webResponse2.GetResponseStream(); StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default, true); string file = reader.ReadToEnd(); }
但是,传输到这个地址“http://<server-ip>/OmEntrance_Rt/Entrance.aspx”代码1正常,但代码2会返回一个错误“远程服务器段返回错误:(500)内部服务器错误”,不明白是怎么回事,请高手指点,谢谢了!
代码1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="http://<server-ip>/OmEntrance_Rt/Entrance.aspx">
<input type="file" name="file" />
<input type="submit" name="Submit" value="提交" />
</form>
</body>
</html>代码2
protected void Button1_Click(object sender, System.EventArgs e)
{
string fullfilename = "d:\\huanbao\\send\\text.xml";
Uri u1 = new Uri("http://<server-ip>/OmEntrance_Rt/Entrance.aspx");
CredentialCache credentialCache1 = new CredentialCache();
this.SendFile(fullfilename, u1, credentialCache1);
} public void SendFile(string fileName, Uri uri, CredentialCache credentialCache)
{
CookieContainer cookies = new CookieContainer();
string boundary = "----------";
HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest2.Credentials = credentialCache;
httpWebRequest2.CookieContainer = cookies;
// httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest2.ContentType = "multipart/form-data";
// httpWebRequest2.ContentType = "application/x-www-form-urlencoded";
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("Content-Type: application/x-xml");
//sb.Append("Content-Type: text/xml");
sb.Append("\r\n");
sb.Append("\r\n"); string postHeader = sb.ToString();
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
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"); httpWebRequest2.ContentLength = boundaryBytes.Length + postHeaderBytes.Length + fileStream.Length; 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))];
byte[] buffer = new Byte[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);
requestStream.Close(); WebResponse webResponse2 = httpWebRequest2.GetResponse(); Stream stream = null; stream = webResponse2.GetResponseStream(); StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default, true); string file = reader.ReadToEnd(); }
解决方案 »
- socket 多个客户端异步发送数据,服务端如何接收数据,并保存下来
- 如何用insertAdjacentHTML添加服务器端控件?
- 新手问题,请大家来看看!页面文字输出相关。
- 如何自动打开文件
- MultiView控件 能不能显示多个view啊
- VS2005,关于WEB安装程序
- 有个问题一直不明白.第一次提问!
- DropDownList3_SelectedIndexChanged总是执行不到,autopostback已经是true
- 在Web控件listBox中显示字串,为何N个连续空格只显示为一个,哪位高人来救火(救命)
- asp.net 如何在web.config配置404错误页面。
- 一组服务器文件下载问题!!!!!!
- 初学ASP.net,设置到IIS上之后一浏览就出现如下问题.请大家帮看看如何处理?
用IE访问下http:// <server-ip>/OmEntrance_Rt/Entrance.aspx看看
最好能把 http:// <server-ip>/OmEntrance_Rt/Entrance.aspx 的代码也贴一下
可能有些参数不对。你可以在Entrance.aspx上设置断点,看一下错误信息。
麻烦大家帮我看看代码2是否有哪个地方需要修改,或是提提意见看问题可能出在哪里。