Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why use this lambda? 12 65
Unlocking a column in excel using C# 17 28
Selenium and Xpath 4 35
Adjust the codes 3 35
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

726 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