Dear Wiki user, You have subscribed to a wiki page or wiki category on "Httpcomponents Wiki" for change notification.
The following page has been changed by OlegKalnichevski: http://wiki.apache.org/HttpComponents/HttpClientTutorial ------------------------------------------------------------------------------ == HTTP parameters == - Remark: most of the content for this chapter can be adopted from HttpCore tutorial. - + HttpParams interface represents a collection of immutable values that define a runtime behavior of a component. In many ways HttpParams is similar to HttpContext. The main distinction between the two lies in their use at runtime. Both interfaces represent a collection of objects that are organized as a map of textual names to object values, but serve distinct purposes: + + * HttpParams is intended to contain simple objects: integers, doubles, strings, collections and objects that remain immutable at runtime. HttpParams is expected to be used in the 'write once - ready many' mode. HttpContext is intended to contain complex objects that are very likely to mutate in the course of HTTP message processing. + + * The purpose of HttpParams is to define a behavior of other components. Usually each complex component has its own HttpParams object. The purpose of HttpContext is to represent an execution state of an HTTP process. Usually the same execution context is shared among many collaborating objects. + === Parameter hierarchies === + In the course of HTTP request execution HttpParams of the HttpRequest object are linked together with HttpParams of the HttpClient instance used to execute the request. This enables parameters set at the HTTP request level take precedence over HttpParams set at the HTTP client level. The recommended practice is to set common parameters shared by all HTTP requests at the HTTP client level and selectively override specific parameters at the HTTP request level. + + {{{ + DefaultHttpClient httpclient = new DefaultHttpClient(); + httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, + HttpVersion.HTTP_1_0); + httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, + "UTF-8"); + + HttpGet httpget = new HttpGet("http://www.google.com/"); + httpget.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, + HttpVersion.HTTP_1_1); + httpget.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, + Boolean.FALSE); + + httpclient.addRequestInterceptor(new HttpRequestInterceptor() { + + public void process( + final HttpRequest request, + final HttpContext context) throws HttpException, IOException { + System.out.println(request.getParams().getParameter( + CoreProtocolPNames.PROTOCOL_VERSION)); + System.out.println(request.getParams().getParameter( + CoreProtocolPNames.HTTP_CONTENT_CHARSET)); + System.out.println(request.getParams().getParameter( + CoreProtocolPNames.USE_EXPECT_CONTINUE)); + System.out.println(request.getParams().getParameter( + CoreProtocolPNames.STRICT_TRANSFER_ENCODING)); + } + + }); + }}} + + stdout> + {{{ + HTTP/1.1 + UTF-8 + false + null + }}} + === HTTP parameters beans === + + HttpParams interface allows for a great deal of flexibility in handling configuration of components. Most importantly, new parameters can be introduced without affecting binary compatibility with older versions. However, HttpParams also has a certain disadvantage compared to regular Java beans: HttpParams cannot be assembled using a DI framework. To mitigate the limitation, HttpCore includes a number of bean classes that can used in order to initialize HttpParams objects using standard Java bean conventions. + + {{{ + HttpParams params = new BasicHttpParams(); + HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params); + paramsBean.setVersion(HttpVersion.HTTP_1_1); + paramsBean.setContentCharset("UTF-8"); + paramsBean.setUseExpectContinue(true); + + System.out.println(params.getParameter( + CoreProtocolPNames.PROTOCOL_VERSION)); + System.out.println(params.getParameter( + CoreProtocolPNames.HTTP_CONTENT_CHARSET)); + System.out.println(params.getParameter( + CoreProtocolPNames.USE_EXPECT_CONTINUE)); + System.out.println(params.getParameter( + CoreProtocolPNames.USER_AGENT)); + }}} + + stdout> + {{{ + HTTP/1.1 + UTF-8 + false + null + }}} = Connection management = --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
