Solved

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

Posted on 2014-12-16
5
99 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

622 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