Solved

Http 404 error while consuming RESTful service in .Net passing data in JSON format

Posted on 2014-12-16
5
79 Views
Last Modified: 2016-05-25
Getting HTTP 404 Error while Consuming RESTful service in .Net passing data in JSON format"

getting HTTP 404 in response at line  also in bold
********************************
 HttpWebResponse response = (HttpWebResponse)request.GetResponse();        
********************************

string json = "{"
                                          + "\"username\" : \"username@email.com\","
                                          + "\"password\" : \"password1@\""
                                          + "}";
                  
                  const string serviceURL = "https://api.wheniwork.com/login";

                  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceURL);
                  
                  request.ContentType = "application/json; charset=utf-8"; //set the content type to JSON
            request.Method = "POST"; //make an HTTP POST
                  //request.Headers.Add("Authorization", "W-Key=key");
                  request.Headers.Add("W-Key", "key");

                  using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                  {
                        //initiate the request
                        JavaScriptSerializer serializer = new JavaScriptSerializer();
                        var resToWrite = serializer.Deserialize<Dictionary<string, object>>(json);
                        streamWriter.Write(resToWrite);
                        streamWriter.Flush();
                        streamWriter.Close();
                  }

                  Stream requestStream = request.GetRequestStream();

                  requestStream.Write(Encoding.ASCII.GetBytes(json), 0, json.Length);
                  requestStream.Close();

            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();              
            var streamReader = new StreamReader(response.GetResponseStream());                
            var result = streamReader.ReadToEnd();
0
Comment
Question by:4eggheads
  • 2
5 Comments
 
LVL 6

Expert Comment

by:oferam
ID: 40503248
404 might be a valid answer - for example if the information you are querying for is not found.

In specific with your sample I think you are missing in the Jason data the key: http://dev.wheniwork.com/#authentication
0
 

Author Comment

by:4eggheads
ID: 40503468
Revised Code--But still does not work.

string json = "{"
+ "\"username\" : \"username@email.com\","
+ "\"password\" : \"password1@\""
+ "}";
const string serviceURL = "https://api.wheniwork.com/login";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceURL);
request.ContentType = "application/json; charset=utf-8"; //set the content type to JSON
            request.Method = "POST"; //make an HTTP POST
//request.Headers.Add("Authorization", "W-Key=key");
request.Headers.Add("W-Key", "key");

using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
//initiate the request
JavaScriptSerializer serializer = new JavaScriptSerializer();
var resToWrite = serializer.Deserialize<Dictionary<string, object>>(json);
streamWriter.Write(resToWrite);
streamWriter.Flush();
streamWriter.Close();
}

// Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();                
            var streamReader = new StreamReader(response.GetResponseStream());                
            var result = streamReader.ReadToEnd();
0
 
LVL 10

Accepted Solution

by:
Walter Padrón earned 500 total points
ID: 40503576
From their doc the url is https://api.wheniwork.com/2/login

Different response, 401 Unauthorized
{"error":"User login required for this resource.","code":1000}
0
 

Author Comment

by:4eggheads
ID: 40511020
We are still trying different combinations, will check back shortly.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question