Reputation: 9891
I need to make a POST request with an xml data.
String xml = "";
byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
HttpClient.post(url, data, "text/xml")
Then I call the POST function:
public static String post(String url, byte[] data, String contentType){
String body = "";
body = getResponse("POST", url, data, contentType, 80);
return body;
}
Now I call this function to make the request / get the response:
public static String getResponse(String method, String url, byte[] data, String contentType, int serverPort)
{
String result = null;
HttpWebRequest request = sendRequest(method, url, data, contentType, serverPort);
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
if (response != null){
// Get the stream associated with the response
Stream receiveStream = response.GetResponseStream ();
// Pipes the stream to a higher level stream reader
StreamReader readStream = new StreamReader (receiveStream, System.Text.Encoding.UTF8);
result = readStream.ReadToEnd ();
}
}
catch(WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError){
throw new HttpClientException("HTTP response error. ", (int)(((HttpWebResponse)ex.Response).StatusCode), ((HttpWebResponse)ex.Response).StatusDescription);
}
else{
throw new HttpClientException("HTTP response error with status: " + ex.Status.ToString());
}
}
}
and
public static HttpWebRequest sendRequest(String method, String url, byte[] data, String contentType, int serverPort){
HttpWebRequest request = null;
try
{
UriBuilder requestUri = new UriBuilder(url);
requestUri.Port = serverPort;
request = (HttpWebRequest)WebRequest.Create(requestUri.Uri);
request.Method = method;
//
if ((method == "POST") && (data != null) && (data.Length > 0)){
request.ContentLength = data.Length;
request.ContentType = ((String.IsNullOrEmpty(contentType))?"application/x-www-form-urlencoded":contentType);
Stream dataStream = request.GetRequestStream ();
dataStream.Write (data, 0, data.Length);
// Close the Stream object.
dataStream.Close ();
}
}
catch(WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError){
throw new HttpClientException("HTTP request error. ", (int)(((HttpWebResponse)ex.Response).StatusCode), ((HttpWebResponse)ex.Response).StatusDescription);
}
else{
throw new HttpClientException("HTTP request error with status: " + ex.Status.ToString());
}
}
}
It always give me an HttpCliendException
:
video_api.HttpClientException: HttpClient exception :HTTP response error. with `code: 400` and `status: Bad Request`
But when I tried it with Firefox addon HTTP Resource Test
, it ran fine and get 202 Accepted status
with the same XML doc.
I consoled the content-type and data.length before the post request was called, the content-type was text/xml and the data.length
is 143
.
Upvotes: 2
Views: 9314
Reputation: 830
Another Solution would be that one messed up SSL and NON-SSL Ports.
When you talk plain http to a https port the answer 400 Bad Request on Apache.
Upvotes: 0
Reputation: 5093
In my project i use some custom settings in the config.area
<defaultProxy useDefaultCredentials="true">
...
</defaultProxy>
It helped me disabling the proxy:
request.Proxy = null;
Upvotes: 0
Reputation: 26290
Use fiddler (http://www.fiddler2.com/fiddler2/) to see exactly what headers are sent by Firefox. Then see what's different in the headers you are sending.
Upvotes: 2
Reputation: 56769
I have known some websites to be picky about request headers and return different results solely based on those values. Compare the request headers of the HTTP request in FireFox, and your request, and if you mimic the headers in the FireFox Resource Test, it will most likely work (Request.AddHeader("name", "value")
). The other difference to note might be the user-agent which again can be picky for web servers.
Upvotes: 2