2

I am very new to programming.

I'm setting up a loop that continuously send a POST request to a site through the REST API. The POST request works properly and the way I intend.

I would like to add functionality that requires the response from this post. However, every way of retrieving the response gives me an error:

"An unhandled exception of type 'System.Net.WebException' occurred in System.dll

Additional information: The remote server returned an error: (400) Bad Request."

As I debug line by line, it seems that the "GetResponse()" line causes this each time, and causes the program to break. If I remove this line, the program works properly and throws no errors.

Hoping someone can assist. Here is what I have written:

string URL= "https:.......";
HttpWebRequest apiCall = (HttpWebRequest)WebRequest.Create(airWatchURL);
apiCall.Method = "POST";
apiCall.Headers.Add(HttpRequestHeader.Authorization, auth);
apiCall.Headers.Add("aw-tenant-code", apiKey);
apiCall.ContentType = "application/json";
apiCall.ContentLength = noBody.Length; //noBody = empty string
Stream c = apiCall.GetRequestStream();
Encoding d = System.Text.Encoding.ASCII;
StreamWriter requestWriter = new StreamWriter(c, d);
requestWriter.Write(noBody);
requestWriter.Close();

WebResponse apiResponse = apiCall.GetResponse(); //This line will return an error.

This will also return the same error:

HttpWebResponse apiResponse = apiCall.GetResponse() as HttpWebResponse;

This will again return the same error:

string status;
using (HttpWebResponse response = apiCall.GetResponse() as HttpWebResponse)
{
    status = response.StatusCode.ToString();
}

I just cant seem to correctly call the GetResponse Method.

Paul Abbott
  • 7,065
  • 3
  • 27
  • 45
Jake K.
  • 21
  • 1
  • 4
  • 1
    You are not causing the error by your `GetResponse` line. The server is sending you an error. `GetResponse` is simply reacting to the error. The question is, what does the server not like about what you are sending it? – John Saunders Nov 05 '14 at 22:02
  • Since you're POSTing, let's see the code where you are setting the POST data. – Paul Abbott Nov 05 '14 at 22:05
  • Paul - Here is the rest of the request. This part appears to work properly, as the data is posted where I'd like. apiCall.Method = "POST"; apiCall.Headers.Add(HttpRequestHeader.Authorization, auth); apiCall.Headers.Add("aw-tenant-code", apiKey); apiCall.ContentType = "application/json"; apiCall.ContentLength = noBody.Length; //noBody = empty string Stream c = apiCall.GetRequestStream(); Encoding d = System.Text.Encoding.ASCII; StreamWriter requestWriter = new StreamWriter(c, d); requestWriter.Write(noBody); requestWriter.Close(); – Jake K. Nov 05 '14 at 22:11
  • `as the data is posted where I'd like` <-- where does it go? what evidence do you have that it goes anywhere correctly? – wal Nov 05 '14 at 22:14
  • Just a guess here, but you're telling the server you're sending json but then sending an empty string. Would sending valid but empty json like "{}" work? I don't understand why you would do a POST and not send any data. – Paul Abbott Nov 05 '14 at 23:24
  • wal - it is posting information into a website. I have validated that the data posts exactly where I'd like it to go. paul - For this post request, all variable data necessary for the post is in the URL itself. – Jake K. Nov 06 '14 at 13:36

0 Answers0