Click here to Skip to main content
15,745,306 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Why my code gives error :The remote server returned an error: (400) Bad Request. error on generating responce
public void GenerateToken(string sRequestId)
    string sRequestContentType = "application/x-www-form-urlencoded,";
    string sBaseUrl = "";
    //string sParameter = "?lifespan=100000" + "&requestId=" + sRequestId;
    string sParameter = "?lifespan=100000&requestId=" + sRequestId;
    sBaseUrl = sBaseUrl + sParameter;       
    string sRes = string.Empty;
    HttpWebRequest oHttpWebRequest = (HttpWebRequest)WebRequest.Create(sBaseUrl);
    oHttpWebRequest.Method = "POST";
    oHttpWebRequest.ContentType = sRequestContentType;
    oHttpWebRequest.UnsafeAuthenticatedConnectionSharing = true;
    oHttpWebRequest.Headers["Authorization"] = "Bearer 624a3eef46f79772958693fa87118b29";
    oHttpWebRequest.Credentials = new NetworkCredential("mrinalkumarjha", "******");    
    // Generating response...    
    HttpWebResponse oHttpWebResponse = (HttpWebResponse)oHttpWebRequest.GetResponse();The remote server returned an error: (400) Bad Request. occur on this line
    StreamReader oStreamReader = new StreamReader(oHttpWebResponse.GetResponseStream());
    sRes = oStreamReader.ReadToEnd();    
    ltrToken.Text = sRes ;
Updated 6-Mar-16 23:51pm
José Amílcar Casimiro 19-Mar-13 18:27pm    
please do yourself a favor and remove yourself your credentials for accessing the service. change the password. best regards.
Bernhard Hiller 21-Mar-13 4:00am    
You set the method to POST, but that looks rather like a GET.
On the other hand, many web servers do not care for the method...
k@ran 25-Mar-13 10:54am    
if you see at api defination it is type of POST.

The 400 Bad Request error is an HTTP status code that means that the request you sent to the website server (e.g. a request to load a web page) was somehow malformed therefore the server was unable to understand or process the request.

Check for errors in the URL. The most common reason for a 400 Bad Request error is because the URL was typed wrong or the link that was clicked on points to a URL with some kind of mistake in it.

Share this answer
Maciej Los 20-Mar-13 15:12pm    
Short and to the point, +5!
_Amy 21-Mar-13 0:08am    
Thank You Maciej. :)
I would go with the following below, and test sBaseUrl in the browser address bar, I think it's the encoding needs to be encoding.utf8, and perhaps the folder token doesn't have a default page for the querystring to attach to.

sBaseUrl = httputility.Urlencode(sBaseUrl + sParameter, encoding.utf8);
Share this answer
400 Bad request exception will cause due to the wrong authentication.
1. check the url which you are sending is valid.
2. check the username and password is write.

note: check whether your leaving any spaces. while copying the contents
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900