1

我正在使用下面的代码将 XML 文件发送到服务器,如果我上传adduser.xml(这是用于添加用户)我得到成功响应,但是对于deleteuser.xml(这是删除用户的用户)我得到

远程服务器返回错误:(400)
错误请求。在 System.Net.HttpWebRequest.GetResponse()
在 DeleteUser.Page_Load(Object sender, EventArgs e)

我已经尝试过方法类型PUTGET但得到了同样POSTDELETE错误。

你能帮我做错什么吗?

 public partial class DeleteUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            WebRequest req = null;
            WebResponse rsp = null;
            try
            {
                string fileName = Server.MapPath("~/deleteuser.xml");
                string uri = "http://test.com/AuctionUsers/delete";
                req = WebRequest.Create(uri);
                req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
                req.Method = "POST";   

                req.ContentType = "application/xml";     // content type

                // Wrap the request stream with a text-based writer
                StreamWriter writer = new StreamWriter(req.GetRequestStream());
                // Write the XML text into the stream
                writer.WriteLine(this.GetTextFromXMLFile(fileName));
                writer.Close();
                // Send the data to the webserver

                rsp = req.GetResponse(); //I am getting error over here
                StreamReader sr = new StreamReader(rsp.GetResponseStream());
                string result = sr.ReadToEnd();
                sr.Close();
                Response.Write(result);

            }
            catch (WebException webEx)
            {
                Response.Write(webEx.Message.ToString());
                Response.Write(webEx.StackTrace.ToString());
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
                Response.Write(ex.StackTrace.ToString());
            }
            finally
            {
                if (req != null) req.GetRequestStream().Close();
                if (rsp != null) rsp.GetResponseStream().Close();
            }
        }

        private string GetTextFromXMLFile(string file)
        {
            StreamReader reader = new StreamReader(file);
            string ret = reader.ReadToEnd();
            reader.Close();
            return ret;
        }
    }

先感谢您。

4

1 回答 1

1

可能有几个不同的原因,一个是方法本身的错误。如果您有权访问,我首先建议在服务器端打开跟踪;它可以帮助您查明错误。引用链接,它应该如下所示:

<configuration>
   <system.diagnostics>
      <sources>
            <source name="System.ServiceModel" 
                    switchValue="Information, ActivityTracing"
                    propagateActivity="true">
            <listeners>
               <add name="traceListener" 
                   type="System.Diagnostics.XmlWriterTraceListener" 
                   initializeData= "c:\log\Traces.svclog" />
            </listeners>
         </source>
      </sources>
   </system.diagnostics>
</configuration>

然后可以从 .NET 跟踪查看器中查看它生成的文件。

编辑:这里有一个类似的帖子,可能会有所帮助:

为什么远程服务器返回错误:(400) Bad Request.?

于 2012-12-28T16:29:38.617 回答