Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi developers I am consuming web api first time .
This is my json object to authenticate a user . how can I pass this asp.net to call web api for getting authenticate token please help me


POST /v2.0/tokens HTTP/1.1
User-Agent: curl/7.21.0 (x86_64-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.15 libssh2/1.2.6
Host: identity.api.rackspacecloud.com
Accept: application/json
Content-Type: application/json
Content-Length: 54

{
"auth":
{
"RAX-KSKEY:apiKeyCredentials":
{
"username": (1)"jsmith",
"apiKey": (2)"aaaaa-bbbbb-ccccc-12345678"
}
}
}

My requirement is that I want to Post this json object to server and get the response from server but I am getting exception of bad request 400. what is the cause of this? I asked to google about this many times and found lots of solution and tried all those but still I am getting same error
I am using this code in asp.net


C#
            // Create a request using a URL that can receive a post. 
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://identity.api.rackspacecloud.com/v2.0/tokens");
          
            request.Method = "POST";
     
            string postData = "{ \"auth\":{\"RAX-KSKEY:apiKeyCredentials\":{\"username\":\"jsmith\" ,\"apikey\":\"aaaaa-bbbbb-ccccc-12345678\" }} }";
          
           
            var byteArray = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(postData));
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = encoding.GetBytes(postData);

           request.Accept = "application/json";

          
           request.ContentType= "application/json";
        
  
             request.ContentLength = data.Length;

          
           Stream dataStream = request.GetRequestStream();
    
           dataStream.Write(data, 0, data.Length);
     dataStream.Close();
            
  Getting exception in this line
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
             // Display the status.
             Console.WriteLine(((HttpWebResponse)response).StatusDescription);
             // Get the stream containing content returned by the server.
           dataStream = response.GetResponseStream();
             // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
             // Read the content.
           string responseFromServer = reader.ReadToEnd();
             // Display the content.
            Console.WriteLine(responseFromServer);
             // Clean up the streams.
             reader.Close();
            dataStream.Close();
             response.Close();

            
        }



please give me some solution .......
Posted
Updated 8-Jun-15 5:16am
v2
Comments
F-ES Sitecore 8-Jun-15 5:07am    
This is your third question on the same subject, please don't repost. You're not getting answers because your questions are vague and unclear. Here, for example, you say you get an exception but don't say what the exception is. We're not mind readers, you need to provide suitable information if you want people to help.
Member 10952217 8-Jun-15 11:10am    
Thanks your response now i am going to update this post with suitable information

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