0

I want am trying to access a HTTP web service using the WebRequest Object in .Net. There are different methods exposed on the HTTP web service and I don't want to duplicate the code for creating Webrequest and Response objects. Is there a way I can do it? I am posting the sample code below.

 WebRequest request = WebRequest.Create("url");
        NetworkCredential myCred = new NetworkCredential("username", "pwd");
        request.Credentials = myCred;
        request.PreAuthenticate = true;

        // Get the response.
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        // Display the status.
        Console.WriteLine(response.StatusDescription);
        // Get the stream containing content returned by the server.
        Stream dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();

        reader.Close();
        dataStream.Close();
        response.Close();

I am thinking whether to create a function that will create Request by passing a uri and Return a response object based upon the request. Is this the right approach? I also don't want to pass the credentials every time. Is it possible?

Any help or reference to some resource would be helpful. Thanks.

2 Answers 2

1

check out Hammock,

https://github.com/danielcrenna/hammock

it's very easy to use and you can install it as a Nuget package.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using Visual Studio, have the visual studio create all the client classes from the web service. You are connecting to web service, not a "web server." In the web service example, you need extra information like the classes and types that need to be created using a tool that reads that information from the "web service." If you are connecting to web site, use httpwebrequest and download the http.

  1. web service it needs to create classes and you call the class

create a project select "add service reference" type in the service address and click create

  1. web server you download html code from a website using httpwebrequest.

2 Comments

My bad. I doesn't not expose any metadata through wsdl or anything like that. I can only manipulate it through urls. I think that can be done only through HttpwebRequest right.
Httpwebrequest is straight up calling a website like a human. I'm not really sure about username and passwords. It will return a ton of html code of the webpage. You can't use web service without the WSDL information. It is a computer interaction. Web service returns a computer language code in the WSDL that the client recreates using a tool ( you can't create the code manually) and the client uses the web service provided class. It is totally different. Web service needs to have WSDL, SOAP information. HTTPwebrequest is for websites like google.com nothing more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.