List of usage examples for org.apache.http.client.methods HttpRequestBase addHeader
public void addHeader(String str, String str2)
From source file:com.expertiseandroid.lib.sociallib.utils.Utils.java
/** * Sends a signed HTTP request using the Signpost library * @param verb the HTTP verb to be used// w w w .ja v a2s.co m * @param request the request's URL * @param consumer the Signpost consumer object * @param contentType contentType the message's content type * @param entity the message's contents * @return the response * @throws OAuthMessageSignerException * @throws OAuthExpectationFailedException * @throws OAuthCommunicationException * @throws ClientProtocolException * @throws IOException */ public static ReadableResponse signedRequest(String verb, String request, CommonsHttpOAuthConsumer consumer, String contentType, String entity) throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, ClientProtocolException, IOException { HttpClient client = new DefaultHttpClient(); HttpRequestBase reqObject = getRequestBaseByVerbEntity(verb, request, entity); consumer.sign(reqObject); reqObject.addHeader("Content-Type", contentType); HttpResponse response = client.execute(reqObject); return new HttpResponseWrapper(response); }
From source file:com.expertiseandroid.lib.sociallib.utils.Utils.java
/** * Sends a signed HTTP request using the Signpost library * @param verb HTTP verb//from w w w .ja v a 2s .c o m * @param request request URI * @param consumer the consumer used to sign the request * @param contentType the content-type of the request * @param bodyParams the body parameters of the request * @return a ReadableResponse to the request * @throws OAuthMessageSignerException * @throws OAuthExpectationFailedException * @throws OAuthCommunicationException * @throws ClientProtocolException * @throws IOException */ public static ReadableResponse signedRequest(String verb, String request, CommonsHttpOAuthConsumer consumer, String contentType, List<NameValuePair> bodyParams) throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, ClientProtocolException, IOException { HttpClient client = new DefaultHttpClient(); HttpRequestBase reqObject = getRequestBaseByVerbParams(verb, request, bodyParams); consumer.sign(reqObject); reqObject.addHeader("Content-Type", contentType); HttpResponse response = client.execute(reqObject); return new HttpResponseWrapper(response); }
From source file:com.squid.kraken.v4.auth.RequestHelper.java
public static <T> T processRequest(Class<T> type, HttpServletRequest request, HttpRequestBase req) throws IOException, URISyntaxException, ServerUnavailableException, ServiceException, SSORedirectException {//from ww w.ja va 2 s. co m // set client information to the header String reqXFF = request.getHeader(STRING_XFF_HEADER); String postXFF; if (reqXFF != null) { // X-Forwarded-For header already exists in the request logger.info(STRING_XFF_HEADER + " : " + reqXFF); if (reqXFF.length() > 0) { // just add the remoteHost to it postXFF = reqXFF + ", " + request.getRemoteHost(); } else { postXFF = request.getRemoteHost(); } } else { postXFF = request.getRemoteHost(); } // add a new X-Forwarded-For header containing the remoteHost req.addHeader(STRING_XFF_HEADER, postXFF); // execute the login request HttpResponse executeCode; try { HttpClient client = HttpClientBuilder.create().build(); executeCode = client.execute(req); } catch (ConnectException e) { // Authentication server unavailable throw new ServerUnavailableException(e); } // process the result BufferedReader rd = new BufferedReader(new InputStreamReader(executeCode.getEntity().getContent())); StringBuffer resultBuffer = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { resultBuffer.append(line); } String result = resultBuffer.toString(); T fromJson; Gson gson = new Gson(); int statusCode = executeCode.getStatusLine().getStatusCode(); if (statusCode != 200) { if (executeCode.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) { String redirectURL = executeCode.getFirstHeader("Location").getValue(); throw new SSORedirectException("SSO Redirect Exception", redirectURL); } else { logger.info("Error : " + req.getURI() + " resulted in : " + result); WebServicesException exception; try { exception = gson.fromJson(result, WebServicesException.class); } catch (Exception e) { if ((statusCode >= 500) && (statusCode < 600)) { // Authentication server unavailable throw new ServerUnavailableException(); } else { throw new ServiceException(); } } throw new ServiceException(exception); } } else { // forward to input page displaying ok message try { fromJson = gson.fromJson(result, type); } catch (Exception e) { throw new ServiceException(e); } } return fromJson; }
From source file:com.payu.sdk.helper.HttpClientHelper.java
/** * Creates a http request with the given request * * @param request//from w w w. j a va 2 s . c o m * The original request * @param requestMethod * The request method to be sent to the server * @return The created http request * @throws URISyntaxException * @throws UnsupportedEncodingException * @throws PayUException * @throws ConnectionException */ private static HttpRequestBase createHttpRequest(Request request, RequestMethod requestMethod) throws URISyntaxException, UnsupportedEncodingException, PayUException, ConnectionException { String url = request.getRequestUrl(requestMethod); URI postUrl = new URI(url); HttpRequestBase httpMethod; LoggerUtil.debug("sending request..."); String xml = Constants.EMPTY_STRING; switch (requestMethod) { case POST: httpMethod = new HttpPost(); break; case GET: httpMethod = new HttpGet(); break; case DELETE: httpMethod = new HttpDelete(); break; case PUT: httpMethod = new HttpPut(); break; default: throw new ConnectionException("Invalid connection method"); } httpMethod.addHeader("Content-Type", MediaType.XML.getCode() + "; charset=utf-8"); Language lng = request.getLanguage() != null ? request.getLanguage() : PayU.language; httpMethod.setHeader("Accept-Language", lng.name()); // Sets the method entity if (httpMethod instanceof HttpEntityEnclosingRequestBase) { xml = request.toXml(); ((HttpEntityEnclosingRequestBase) httpMethod) .setEntity(new StringEntity(xml, Constants.DEFAULT_ENCODING)); LoggerUtil.debug("Message to send:\n {0}", xml); } httpMethod.setURI(postUrl); addRequestHeaders(request, httpMethod); LoggerUtil.debug("URL to send:\n {0}", url); return httpMethod; }
From source file:com.tripit.auth.WebAuthCredential.java
public void authorize(HttpRequestBase request) throws Exception { request.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(new String(this.username + ":" + this.password).getBytes()))); }
From source file:ca.uhn.fhir.rest.client.interceptor.CookieInterceptor.java
@Override public void interceptRequest(HttpRequestBase theRequest) { theRequest.addHeader(Constants.HEADER_COOKIE, sessionCookie); //$NON-NLS-1$ }
From source file:v2.service.generic.library.utils.HttpClientUtil.java
public static HttpResponsePOJO jsonRequest(String url, String json, String requestType, Map<String, String> headers) throws Exception { HttpClient httpclient = createHttpClient(); // HttpEntityEnclosingRequestBase request=null; HttpRequestBase request = null; if ("POST".equalsIgnoreCase(requestType)) { request = new HttpPost(url); } else if ("PUT".equalsIgnoreCase(requestType)) { request = new HttpPut(url); } else if ("GET".equalsIgnoreCase(requestType)) { request = new HttpGet(url); } else if ("DELETE".equalsIgnoreCase(requestType)) { request = new HttpDelete(url); }//from w ww .java2s.c om if (json != null) { if (!"GET".equalsIgnoreCase(requestType) && (!"DELETE".equalsIgnoreCase(requestType))) { StringEntity params = new StringEntity(json, "UTF-8"); params.setContentType("application/json;charset=UTF-8"); ((HttpEntityEnclosingRequestBase) request).setEntity(params); } } if (headers == null) { request.addHeader("content-type", "application/json"); } else { Set<String> keySet_header = headers.keySet(); for (String key : keySet_header) { request.setHeader(key, headers.get(key)); } } HttpResponsePOJO result = invoke(httpclient, request); return result; }
From source file:v2.service.generic.library.utils.HttpClientUtil.java
public static ResponsePOJO nativeRequest(String url, String json, String requestType, Map<String, String> headers) throws Exception { HttpClient httpclient = createHttpClient(); // HttpEntityEnclosingRequestBase request=null; HttpRequestBase request = null; if ("POST".equalsIgnoreCase(requestType)) { request = new HttpPost(url); } else if ("PUT".equalsIgnoreCase(requestType)) { request = new HttpPut(url); } else if ("GET".equalsIgnoreCase(requestType)) { request = new HttpGet(url); } else if ("DELETE".equalsIgnoreCase(requestType)) { request = new HttpDelete(url); }/*from w w w. j av a2 s .c o m*/ if (json != null) { if (!"GET".equalsIgnoreCase(requestType) && (!"DELETE".equalsIgnoreCase(requestType))) { StringEntity params = new StringEntity(json, "UTF-8"); // StringEntity s = new StringEntity(jsonString, "UTF-8"); ((HttpEntityEnclosingRequestBase) request).setEntity(params); } } if (headers == null) { request.addHeader("content-type", "application/json"); } else { Set<String> keySet_header = headers.keySet(); for (String key : keySet_header) { request.setHeader(key, headers.get(key)); } } ResponsePOJO result = nativeInvoke(httpclient, request); return result; }
From source file:com.smartsheet.api.sdk_test.TestHttpClient.java
@Override public HttpRequestBase createApacheRequest(HttpRequest smartsheetRequest) { HttpRequestBase apacheHttpRequest = super.createApacheRequest(smartsheetRequest); apacheHttpRequest.addHeader("Api-Scenario", apiScenario); return apacheHttpRequest; }
From source file:ca.uhn.fhir.rest.client.interceptor.UserInfoInterceptor.java
@Override public void interceptRequest(HttpRequestBase theRequest) { if (myUserId != null) theRequest.addHeader(HEADER_USER_ID, myUserId); if (myUserName != null) theRequest.addHeader(HEADER_USER_NAME, myUserName); if (myAppName != null) theRequest.addHeader(HEADER_APPLICATION_NAME, myAppName); }