Reputation: 584
I am using below code for send XML file to server, if I upload adduser.xml
(this is using for adding users) I am getting success response, but for deleteuser.xml
(this is user for delete the user) I am getting
The remote server returned an error: (400)
Bad Request. at System.Net.HttpWebRequest.GetResponse()
at DeleteUser.Page_Load(Object sender, EventArgs e)
I have tried method types PUT
, GET
, POST
and DELETE
but got the same error.
Can you plz help me what I am doing wrong?
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;
}
}
Thank you in advance.
Upvotes: 1
Views: 743
Reputation: 3970
There may be several different reasons, one being an error on the method itself. I would first advice to turn tracing on server-side, if you have access; it may help you pinpoint errors. Quoting the link, it should look like this:
<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>
The file it generates can then be viewed from within the .NET trace viewer.
EDIT: There's a similar post here, which can be of some help:
Why The remote server returned an error: (400) Bad Request.?
Upvotes: 1