Solved

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

Posted on 2014-12-16
5
66 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
To properly understand GitHub, let’s divide it into two words ‘Git’ and ‘Hub’. Git is basically a ‘Distribution Version Control’ (DVC) and ‘Source Code Management’ (SCM) system widely used by software programmers while Hub means the efficient centre…
The viewer will learn how to count occurrences of each item in an array.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now