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 interfac开发者_StackOverflow社区e 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
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
精彩评论