Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-12-16
5
Medium Priority
?
112 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 2000 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

Automating Your MSP Business

The road to profitability.
Delivering superior services is key to ensuring customer satisfaction and the consequent long-term relationships that enable MSPs to lock in predictable, recurring revenue. What's the best way to deliver superior service? One word: automation.

Question has a verified solution.

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

Online collaboration is quickly becoming embedded in the workplace, and its benefits are tangible. See what the current landscape looks like and what the future holds for collaboration tools and the future of work.
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.
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…
The viewer will learn how to dynamically set the form action using jQuery.

715 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