我正在使用下面的代码将 XML 文件发送到服务器,如果我上传adduser.xml
(这是用于添加用户)我得到成功响应,但是对于deleteuser.xml
(这是删除用户的用户)我得到
远程服务器返回错误:(400)
错误请求。在 System.Net.HttpWebRequest.GetResponse()
在 DeleteUser.Page_Load(Object sender, EventArgs e)
我已经尝试过方法类型PUT
,GET
但得到了同样POST
的DELETE
错误。
你能帮我做错什么吗?
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;
}
}
先感谢您。