0

I am writing a c# program for HttpRequest and Response as below.

public string Methods(int id) { HttpWebRequest Request = (HttpWebRequest) WebRequest.Create(@"http://172.17.18.16:8082/item/detail/90"); Request.Method = "Get"; WebResponse Response = Request.GetResponse(); IHttpRequest request = (IHttpRequest)Request; IHttpResponse response = (IHttpResponse)Response; SetupRequest(request, response, session); //Request.Method = Method.Get; string m = request.Method; TemplateManager mg=new TemplateManager(); ItemController it = new ItemController(mg); it.Detail(); return m; } 

Here IHttpRequest and IHttpResponse are the Interfaces which are already created. I want to assign HttpWebRequest Request to the interface so that the remaining functionality should happen itself. I am getting error in casting lines as

Unable to cast object of type 'System.Net.HttpWebRequest' to type 'HttpServer.IHttpRequest'.

Please help me to find a solution

Thanks

3
  • 1
    No idea by what you mean by remaining functionality should happen itself. What do you mean by IHttpRequest and IHttpResponse are already created ? Commented May 25, 2011 at 10:57
  • In HttpRequest class all the code is already written to perform whatever task.i am creating a new window ui and passing url.so this url get passed to ihttprequest and perform whatever task they written ther Commented May 25, 2011 at 11:02
  • 1
    If I understand you correctly you have created an interface that you want the System.Net.HttpWebRequest to implement which is difficult. But perhaps you could post the interface? Your solution would probably be to create an adapter that implements your interface and uses composition to redirect to the received HttpWebResponse. Commented May 25, 2011 at 11:03

1 Answer 1

1

These lines are never going to work:

IHttpRequest request = (IHttpRequest)Request; IHttpResponse response = (IHttpResponse)Response; 

because neither of them implement the interfaces IHttpRequest or IHttpResponse. To make an inbuilt class implement your own interfaces you will have to extend it (derive a new class from it), I'm not sure exactly why you are trying to do this with these two classes, but typically you would only pass them around as an interface if you wanted to change how they are implemented; i.e. if you wanted to write a replacement for HttpWebRequest, or if you were using a dependency injection container (and even this doesn't require interfaces to work).

Check here for their doco online: HttpWebRequest & HttpWebResponse

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.