代码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();        }

解决方案 »

  1.   

    你先要保证ie能够顺利打开你webrequest指定的网址
      

  2.   

    你服务器配置对了吗?
    用IE访问下http:// <server-ip>/OmEntrance_Rt/Entrance.aspx看看
      

  3.   

    用IE直接访问http://<server-ip>/OmEntrance_Rt/Entrance.aspx会出错,用代码1的页面提交跳转到http://<server-ip>/OmEntrance_Rt/Entrance.aspx就正常,我想不明白的就是这个。
      

  4.   

    intertnet选项-高级-显示友好的http错误信息 把这个选上,看看报什么错
      

  5.   


    最好能把 http:// <server-ip>/OmEntrance_Rt/Entrance.aspx 的代码也贴一下
    可能有些参数不对。你可以在Entrance.aspx上设置断点,看一下错误信息。
      

  6.   

    http:// <server-ip>/OmEntrance_Rt/Entrance.aspx的代码不是我写的,我也不知道里面具体写的是什么。
    麻烦大家帮我看看代码2是否有哪个地方需要修改,或是提提意见看问题可能出在哪里。