java apache HC BasicHttpEntityEnclosingRequest vs BasicHttpRequest

六月ゝ 毕业季﹏ 提交于 2019-12-23 04:11:33

问题


Making a class that takes instructions (steps) from a data source and automates few HC (http://hc.apache.org Apache Http Client) actions.

Want to be able to carry out HTTP post, get and direct (like sending a JSON or XML/ soap message to a web server) calls.

Confused about when to use BasicHttpEntityEnclosingRequest vs BasicHttpReques what is the enclosing? Right now I have steps to init the context, provide param values, the URL, method etc when i want to submit this is what I'm doing:

        HttpPost httpost = null;//todo correct method
        //HttpEntity resp = this.httpclient.e

        HttpEntityEnclosingRequest reqEntity1 = null;//use this
        HttpRequest reqEntity = new BasicHttpRequest(method, urls, httpVer );//or this?
        URL url = new URL(urls);
        String hostNm = url.getHost();
        int port = url.getPort();
        String sche = url.getProtocol();
        logger.info("scheme/ proto :" + sche);
        HttpHost  httpHost = new HttpHost (hostNm, port, sche);
        response = this.httpclient.execute(httpHost, reqEntity, localContext);

Question : use one or the other or have another attribute for enclosing or regular HttpRequest?

See

http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpRequest.html

http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/message/BasicHttpRequest.html

http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpEntityEnclosingRequest.html


回答1:


HTTP specification clearly defines only POST and PUT as methods which can enclose a request content body. Whether or not it is legal for other methods such as GET or HEAD to enclose a request body is subject to debate.

HttpCore follows strict interpretation of the HTTP specification and represent regular requests as HttpRequest which does not provide a method to set a content body. For methods such as PUT and POST it provides extended HttpEntityEnclosingRequest with extra methods for request entity manipulations.



来源:https://stackoverflow.com/questions/18419268/java-apache-hc-basichttpentityenclosingrequest-vs-basichttprequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!