简体   繁体   English

HttpWebRequest 和响应

[英]HttpWebRequest and Response

I am writing a c# program for HttpRequest and Response as below.我正在为HttpRequestResponse编写一个 c# 程序,如下所示。

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.这里IHttpRequestIHttpResponse是已经创建的接口。 I want to assign HttpWebRequest Request to the interface so that the remaining functionality should happen itself.我想将HttpWebRequest Request分配给接口,以便剩余的功能应该自己发生。 I am getting error in casting lines as我在铸造线时遇到错误

Unable to cast object of type 'System.Net.HttpWebRequest' to type 'HttpServer.IHttpRequest'.无法将“System.Net.HttpWebRequest”类型的 object 转换为“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 .因为它们都没有实现接口IHttpRequestIHttpResponse 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;要使内置 class 实现您自己的接口,您必须扩展它(从中派生一个新的 class),我不确定您为什么要尝试使用这两个类来执行此操作,但通常您只会将它们传递如果你想改变它们的实现方式,作为一个接口; ie 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).即,如果您想为HttpWebRequest编写替代品,或者您正在使用依赖注入容器(甚至这不需要接口来工作)。

Check here for their doco online: HttpWebRequest & HttpWebResponse在此处查看他们的在线文档: HttpWebRequestHttpWebResponse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM