List of usage examples for org.apache.http.client.methods HttpUriRequest setHeader
void setHeader(String str, String str2);
From source file:com.demo.wtm.service.RestService.java
private HttpUriRequest addHeaderParams(HttpUriRequest request) throws Exception { for (NameValuePair h : headers) { request.addHeader(h.getName(), h.getValue()); }//www. java2 s.c o m WtmPreference wp = new WtmPreference(context); authtoken = wp.getString("authtoken", ""); if (authentication) { request.setHeader("X-ZUMO-APPLICATION", "wMaoPxwwslHHzDTejLrObxTljWXxvT36"); request.setHeader("X-ZUMO-AUTH", authtoken); } return request; }
From source file:se.inera.certificate.proxy.mappings.remote.RemoteDispatcher.java
private void addHeaders(HttpServletRequest request, HttpUriRequest req) { for (String headerName : list(request.getHeaderNames())) { if (!BLOCKED_HEADERS.contains(lowerCase(headerName))) { req.addHeader(headerName, request.getHeader(headerName)); }// w w w . j av a 2s . c o m } for (Map.Entry<String, String> headers : getRequestContext().getHeaders(request).entrySet()) { req.setHeader(headers.getKey(), headers.getValue()); } }
From source file:com.lightbox.android.network.HttpHelper.java
/** * Call the specified {@link HttpMethod}. * @param method/*w w w . j av a 2 s . c o m*/ * @param uri * @param urlParameters Optional. * @param body Optional. If you pass a Map, it will be passed as Form-URLEncoded, if you pass an {@link HttpEntity} * it will be used directly, otherwise the toString will be used. * @param headers Optional. * @return the {@link HttpResponse} * @throws IOException if the request cannot be sent. */ public HttpResponse call(HttpMethod method, URI uri, Map<String, Object> urlParameters, Object body, Map<String, String> headers) throws IOException { // Add query parameters uri = addQueryParametersToUri(uri, urlParameters); // Create the http request HttpUriRequest httpRequest = method.createHttpRequest(uri); // Add body httpRequest = addBodyToHttpRequest(httpRequest, body); // Set language httpRequest.setHeader("Accept-Language", Locale.getDefault().toString().replace('_', '-')); // Set headers if (headers != null) { for (Map.Entry<String, String> header : headers.entrySet()) { httpRequest.setHeader(header.getKey(), header.getValue()); } } // Send the request DebugLog.d(TAG, "Calling %s", uri.toString()); HttpResponse httpResponse = mHttpClient.execute(httpRequest); // Check for redirect final int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_MOVED_PERMANENTLY) { Header locationHeader = httpResponse.getFirstHeader("Location"); if (locationHeader != null) { String newUrl = locationHeader.getValue(); // call again with new URL return call(method, URI.create(newUrl), urlParameters, body, headers); } } // return a httpResponse return httpResponse; }
From source file:com.evrythng.java.wrapper.core.api.ApiCommand.java
/** * Builds and prepares the {@link HttpUriRequest}. * * @param method the {@link MethodBuilder} used to build the request * @return the prepared {@link HttpUriRequest} for execution *//* w w w. j a v a 2 s .c o m*/ private HttpUriRequest buildRequest(final MethodBuilder<?> method) throws EvrythngClientException { // Build request method: HttpUriRequest request = method.build(uri()); // Define client headers: for (Entry<String, String> header : headers.entrySet()) { request.setHeader(header.getKey(), header.getValue()); } return request; }
From source file:com.fujitsu.dc.test.jersey.DcRestAdapter.java
/** * ????GET./* w ww . jav a 2s.c om*/ * @param url URL * @param headers ?? * @return DcResponse * @throws DcException DAO */ public final DcResponse get(final String url, final HashMap<String, String> headers) throws DcException { HttpUriRequest req = new HttpGet(url); for (Map.Entry<String, String> entry : headers.entrySet()) { req.setHeader(entry.getKey(), entry.getValue()); } req.addHeader("X-Dc-Version", DcCoreTestConfig.getCoreVersion()); debugHttpRequest(req, ""); DcResponse res = this.request(req); return res; }
From source file:com.loopj.android.http.sample.RangeResponseSample.java
@Override public ResponseHandlerInterface getResponseHandler() { return new RangeFileAsyncHttpResponseHandler(file) { @Override//from www .ja v a2 s. c o m public void onSuccess(int statusCode, Header[] headers, File file) { debugHeaders(LOG_TAG, headers); debugStatusCode(LOG_TAG, statusCode); if (fileSize < 1) { boolean supportsRange = false; // Cycle through the headers and look for the Content-Length header. for (Header header : headers) { String headerName = header.getName(); if (CONTENT_LENGTH.equals(headerName)) { fileSize = Long.parseLong(header.getValue()); } else if (ACCEPT_RANGES.equals(headerName)) { supportsRange = true; } } // Is the content length known? if (!supportsRange || fileSize < 1) { Toast.makeText(RangeResponseSample.this, "Unable to determine remote file's size, or\nremote server doesn't support ranges", Toast.LENGTH_LONG).show(); } } // If remote file size is known, request next portion. if (fileSize > 0) { debugFileResponse(file); // Send a new request for the same resource. sendNextRangeRequest(); } } @Override public void onFailure(int statusCode, Header[] headers, Throwable e, File file) { debugHeaders(LOG_TAG, headers); debugStatusCode(LOG_TAG, statusCode); debugThrowable(LOG_TAG, e); debugFileResponse(file); } @Override public void updateRequestHeaders(HttpUriRequest uriRequest) { // Call super so appending could work. super.updateRequestHeaders(uriRequest); // Length of the downloaded content thus far. long length = file.length(); // Request the next portion of the file to be downloaded. uriRequest.setHeader("Range", "bytes=" + length + "-" + (length + CHUNK_SIZE - 1)); } void debugFileResponse(File file) { debugResponse(LOG_TAG, "File size thus far: " + file.length() + " bytes"); } }; }
From source file:io.personium.test.jersey.PersoniumRestAdapter.java
/** * ????GET./*w w w . ja va 2 s . c o m*/ * @param url URL * @param headers ?? * @return DcResponse * @throws PersoniumException DAO */ public final PersoniumResponse get(final String url, final HashMap<String, String> headers) throws PersoniumException { HttpUriRequest req = new HttpGet(url); for (Map.Entry<String, String> entry : headers.entrySet()) { req.setHeader(entry.getKey(), entry.getValue()); } req.addHeader("X-Personium-Version", PersoniumCoreTestConfig.getCoreVersion()); debugHttpRequest(req, ""); PersoniumResponse res = this.request(req); return res; }
From source file:com.liferay.ide.core.remote.RemoteConnection.java
protected String getHttpResponse(HttpUriRequest request) throws Exception { if (!CoreUtil.isNullOrEmpty(getUsername()) && !CoreUtil.isNullOrEmpty(getPassword())) { String encoding = getUsername() + ":" + getPassword(); request.setHeader("Authorization", "Basic " + Base64.encodeBase64String(encoding.getBytes())); }// w ww. j a v a2 s .c o m HttpResponse response = getHttpClient().execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); String body = CoreUtil.readStreamToString(entity.getContent(), false); EntityUtils.consume(entity); return body; } else { return response.getStatusLine().getReasonPhrase(); } }
From source file:io.personium.jersey.engine.test.ScriptTestBase.java
/** * .//from ww w .j a va 2 s . c o m * @param url url */ private void callServiceTest(final String url) { try { HttpUriRequest req = new PersoniumRequestBuilder().url(url).method("GET").token(token).build(); req.setHeader(KEY_HEADER_BASEURL, baseUrl); String version = getVersion(); if (version != null && !(version.equals(""))) { req.setHeader("X-Personium-Version", version); } PersoniumResponse res = request(req); assertEquals(HttpStatus.SC_OK, res.getStatusCode()); assertEquals("OK", res.bodyAsString()); } catch (DaoException e) { fail(e.getMessage()); } }
From source file:ss.udapi.sdk.services.HttpServices.java
public ServiceRequest processLogin(ServiceRequest loginReq, String relation, String name) throws Exception { if (loginReq == null) throw new IllegalArgumentException("loginReq must be a valid request"); logger.info("Preparing request for: " + name); CloseableHttpClient httpClient = HttpClients.custom().setKeepAliveStrategy(loginTimeout).build(); List<RestItem> serviceRestItems = null; ServiceRequest serviceRequest = new ServiceRequest(); RestItem loginDetails = getRestItems(loginReq, name); if (loginDetails == null) { logger.error("Link not found for request " + name); return null; }//from ww w .j av a 2 s.com RestLink link = getLink(loginDetails, relation); if (link == null) { logger.error("Relation not found for relation: " + relation + " for " + name); return null; } CloseableHttpResponse response = null; try { HttpUriRequest httpAction = new HttpPost(link.getHref()); if (compressionEnabled == true) { httpAction.setHeader("Accept-Encoding", "gzip"); } httpAction.setHeader("X-Auth-User", SystemProperties.get("ss.username")); httpAction.setHeader("X-Auth-Key", SystemProperties.get("ss.password")); httpAction.setHeader("Content-Type", "application/json"); // Send the request of to retrieve the Authentication Token and the // list of available services. response = httpClient.execute(httpAction); if (response.getStatusLine().getStatusCode() != 200) { throw new ClientProtocolException("Unexpected response status: " + response.getStatusLine().getStatusCode() + " while retrieving services for: " + name); } if (response.getFirstHeader("X-Auth-Token") == null) throw new ClientProtocolException("Unexpected response: no auth token found"); serviceAuthToken = response.getFirstHeader("X-Auth-Token").getValue(); serviceRequest.setAuthToken(serviceAuthToken); HttpEntity entity = response.getEntity(); String jsonResponse = new String(EntityUtils.toByteArray(entity)); serviceRestItems = JsonHelper.toRestItems(jsonResponse); serviceRequest.setServiceRestItems(serviceRestItems); return serviceRequest; } catch (ClientProtocolException protEx) { logger.error( "Invalid Client Protocol: " + protEx.getMessage() + " while retrieving services for: " + name); throw protEx; } catch (IOException ioEx) { logger.error("Communication error" + ioEx.getCause() + " while retrieving services for: " + name); throw ioEx; } finally { try { httpClient.close(); } catch (IOException ex) { // Can safely be ignored, either the server closed the // connection or we didn't open it so there's nothing to do } } }