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.