Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am new to using the Restful protocol and when I attempt to retrieve the response, I get an error.

Heres the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Text.RegularExpressions;
using System.IO;
using System.Net;
//using System.Net.Configuration;
//using System.Web.Services;
//using System.Web;
//using System.Collections;
//using System.Web.Services.Protocols;
//using System.Web.Script.Services;
//using System.Xml;

namespace ConsoleApplication27
{
    class Program
    {
        static void Main(string[] args)       
        {

// Create the web request  
            try
            {
                HttpWebRequest request = WebRequest.Create("https://ERS123.optimahcs.com/cpapi/v2/clinical/patients/12377") as HttpWebRequest;
                request.Method = "GET";
                request.Credentials = new NetworkCredential("ERS123clinicalapi", "###########");
// the next line generates exception [System.Net.WebException] = {"The remote server returned an error: (401) Unauthorized."}
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
                {
                    // Get the response stream  
                    StreamReader reader = new StreamReader(response.GetResponseStream());

                    // Console application output  
                    Console.WriteLine(reader.ReadToEnd());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}


This works fine when I used Fiddler.

Any ideas?

Thank you.
Posted
Updated 28-Oct-13 11:38am
v2

I just added the following change to my code:

HttpWebRequest request = WebRequest.Create("https://bayden.com/sandbox/") as HttpWebRequest;
request.Method = "GET";


And get a valid response:

XML
<html>
<head>
<script src="sample.js"></script>
</head>

<body>

<h1>HTTP Sandbox</h1>
<p>Welcome to the HTTP Sandbox.&nbsp; This page allows you to manipulate HTTP
requests and responses without fear of damaging live data.</p>
<p>These pages are designed to be very simple to allow you to concentrate on the
task at hand.</p>
<p>You can get to the HTTPS version of this page at
<a href="https://bayden.com/sandbox/">https://bayden.com/sandbox/</a> </p>
<h2>&nbsp;</h2>
<h2>Sandboxes</h2>
<ul>
        <li><a href="shop/">Shopping cart example</a></li>
        <li><a href="status.asp">
        Response Status Code Generator</a></li>
<li><a href="FormAndCookie.asp">Upload form, and cookie tester</a></li>
        <li><a href="FileForm.asp">Forms</a></li>
        <li><a href="scriptpage.asp">Script Page</a><br>
&nbsp;</li>
</ul>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

</body>

</html>

My problem has to do with validation. can any one show me how to send a get with validation 
username and password?
 
Share this answer
 
Comments
VICK 31-Oct-13 1:50am    
If this solved your problem, than kindly accept it as solution to close the question.
SQL
Hey!
I think it is a credential related error. Add Credentials for HttpWebRequest

For more info visit
http://www.c-sharpcorner.com/Forums/Thread/298568/ShowMessages.aspx?ThreadID=298568</a>[^]
 
Share this answer
 
Thank you for pointing me to these other posts. For this application, I am only looking to provide Restful client side functionality so I won't need to use IIS or a web.config file. A couple a months ago I created a Web Client side WDSL service as a console app and it works fine. In the case of Rest, this is my first time writing a program to retrieve data from a Restful Web service. I suspect there's some thing basic I am missing, If you could point me to any on-line videos or documentation showing how to retieve the data and how to specify the format I want to get in back in, (json, xml,PDF) that would be very helpful.

Thank you.
 
Share this answer
 
Comments
VICK 31-Oct-13 1:49am    
Have you seen "Have a Question or Comment" Button under the solution posted by ThatRaja???

if yes, than kindly use that to post a comment to him, so that he could get a notification of your reply and give your response if required. "Add a Solution" option is not for this purpose.

hope you got the point.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900