本文整理汇总了Java中org.apache.http.HttpResponse.addHeader方法的典型用法代码示例。如果您正苦于以下问题:Java HttpResponse.addHeader方法的具体用法?Java HttpResponse.addHeader怎么用?Java HttpResponse.addHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.HttpResponse
的用法示例。
在下文中一共展示了HttpResponse.addHeader方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: executePost
import org.apache.http.HttpResponse; //导入方法依赖的package包/类
/**
* Выполнить Post запрос
*
* @param <T>
* класс ответа {@link ru.maksimov.andrey.golos4j.dto.api}
*
* @param method
* объект который нужно отпрапвить в запросе
* @param classDto
* класс возврата
* @param url
* url адрес
* @return дата в заданном формате
* @throws SystemException
* система ошибка выполения запроса
*/
public static <T> T executePost(BaseMethod method, Class<T> classDto, String url) throws SystemException {
SSLContext sslContext = Util.getSSLContext();
RequestConfig config = Util.getConfig(connectTimeout, connectionRequestTimeout, socketTimeout);
CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLContext(sslContext)
.setDefaultRequestConfig(config).build();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(method.getEntity());
httpPost.addHeader("Content-Type", APPLICATION_JSON_UTF8_VALUE);
try {
HttpResponse response = httpClient.execute(httpPost);
response.addHeader("Content-Type", APPLICATION_JSON_UTF8_VALUE);
HttpEntity entity = response.getEntity();
LOG.debug("Content: " + entity.getContent());
if (entity != null) {
LOG.debug("Response content length: " + entity.getContentLength());
}
ObjectMapper mapper = new ObjectMapper();
String jsonString = EntityUtils.toString(entity, ENCODING_CHARSET);
LOG.debug("Response content: " + jsonString);
T getDto = mapper.readValue(jsonString, classDto);
return getDto;
} catch (ClientProtocolException cpe) {
throw new SystemException("Unable execute send POST-request: " + cpe.getMessage(), cpe);
} catch (IOException ioe) {
throw new SystemException("Unable execute POST-request: " + ioe.getMessage(), ioe);
}
}
示例2: process
import org.apache.http.HttpResponse; //导入方法依赖的package包/类
public void process(final HttpResponse response, final HttpContext context)
throws HttpException, IOException {
if (response == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (!response.containsHeader(HTTP.SERVER_HEADER)) {
String s = (String) response.getParams().getParameter(
CoreProtocolPNames.ORIGIN_SERVER);
if (s != null) {
response.addHeader(HTTP.SERVER_HEADER, s);
}
}
}
示例3: handle
import org.apache.http.HttpResponse; //导入方法依赖的package包/类
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
throws HttpException, IOException {
response.setStatusCode(HttpStatus.SC_MOVED_PERMANENTLY);
response.addHeader("Location", PropagationHandler.MAPPING);
}
示例4: build
import org.apache.http.HttpResponse; //导入方法依赖的package包/类
public static DummyResponseServerBehavior build(int statusCode, String statusMessage, String content) {
HttpResponse response = new BasicHttpResponse(
new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), statusCode, statusMessage));
setEntity(response, content);
response.addHeader("Content-Length", String.valueOf(content.getBytes().length));
response.addHeader("Connection", "close");
return new DummyResponseServerBehavior(response);
}
示例5: testPostAuthForCookie
import org.apache.http.HttpResponse; //导入方法依赖的package包/类
/**
* Tests the authentication post.
*/
private void testPostAuthForCookie(int responseStatus, String headerName, boolean isValidUser) throws IOException
{
HttpResponse fakeHttpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, responseStatus, "some reason");
fakeHttpResponse.addHeader(headerName, COOKIE_VALUE);
fakeHttpResponse.setEntity(new StringEntity("{\"isLoggedIn\": " + isValidUser + "}"));
when(mockResponse.returnResponse()).thenReturn(fakeHttpResponse);
NameValuePair[] authParams = new NameValuePair[] {
new BasicNameValuePair("auth1", "hello"),
new BasicNameValuePair("auth2", "world")
};
httpHelper.postAuthForCookie(mockExecutor, URI, authParams);
}
示例6: getResponse
import org.apache.http.HttpResponse; //导入方法依赖的package包/类
@Override
public HttpResponse getResponse(Request r) throws IOException {
HttpResponse response = parentAction.getResponse(r);
response.addHeader(name, value);
return response;
}
示例7: process
import org.apache.http.HttpResponse; //导入方法依赖的package包/类
/**
* Processes the response (possibly updating or inserting) Content-Length and Transfer-Encoding headers.
* @param response The HttpResponse to modify.
* @param context Unused.
* @throws ProtocolException If either the Content-Length or Transfer-Encoding headers are found.
* @throws IllegalArgumentException If the response is null.
*/
public void process(final HttpResponse response, final HttpContext context)
throws HttpException, IOException {
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
if (this.overwrite) {
response.removeHeaders(HTTP.TRANSFER_ENCODING);
response.removeHeaders(HTTP.CONTENT_LEN);
} else {
if (response.containsHeader(HTTP.TRANSFER_ENCODING)) {
throw new ProtocolException("Transfer-encoding header already present");
}
if (response.containsHeader(HTTP.CONTENT_LEN)) {
throw new ProtocolException("Content-Length header already present");
}
}
ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
if (entity.isChunked() && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
response.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
} else if (len >= 0) {
response.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
}
// Specify a content type if known
if (entity.getContentType() != null && !response.containsHeader(
HTTP.CONTENT_TYPE )) {
response.addHeader(entity.getContentType());
}
// Specify a content encoding if known
if (entity.getContentEncoding() != null && !response.containsHeader(
HTTP.CONTENT_ENCODING)) {
response.addHeader(entity.getContentEncoding());
}
} else {
int status = response.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_NO_CONTENT
&& status != HttpStatus.SC_NOT_MODIFIED
&& status != HttpStatus.SC_RESET_CONTENT) {
response.addHeader(HTTP.CONTENT_LEN, "0");
}
}
}