Reputation: 101
var url = ConfigurationManager.AppSettings["URL"] + "/Archivador/MoverEnvio";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
if (Certificado != null)
{
// Y añadirlo a la petición HTTP
req.ClientCertificates.Add(Certificado);
}
req.Method = "PUT";
req.ContentType = "application/json";
ArchivadorModelPut Mover = new ArchivadorModelPut()
{
ID_ARCHIVADOR = idArchivador,
ID_ENVIO = idEnvio
};
using (var streamWriter = new StreamWriter(req.GetRequestStream()))
{
string json = JsonConvert.SerializeObject(Mover);
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
StreamReader reader = new StreamReader(resp.GetResponseStream());
var dato = reader.ReadToEnd();
string returnString = resp.StatusCode.ToString();
}
I got this Exception:
System.Net.WebException: 'Error on the remote server: (400) Bad Request.'
On line:
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
I don't know how to resolve the problem, can someone help me? If you need more information about it, let me know.
Upvotes: 1
Views: 8604
Reputation: 86729
This exception means that the request was completed successfully, however a 400 HTTP response was returned. A 400 HTTP response normally means that your client did not send the correct data, however its completely up to the destination server under what circumstances a 400 response would be returned.
If you catch the WebException
you can use the Response property to look at the HTTP response - depending on the server it might contain a description of the error.
If its normal practice for the destination service to return a 400 response then take a look at Why does HttpWebRequest throw an exception instead returning HttpStatusCode.NotFound? for an extension method that can be used to get the response regardless of the status code. Alternatively you could use an alternative class that doesn't throw, e.g. HttpClient
.
Upvotes: 1