开发者

HttpWebRequest and Response

开发者 https://www.devze.com 2023-03-08 01:58 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消