Example usage for org.apache.http.client.methods HttpUriRequest setHeader

List of usage examples for org.apache.http.client.methods HttpUriRequest setHeader

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpUriRequest setHeader.

Prototype

void setHeader(String str, String str2);

Source Link

Usage

From source file:com.mmj.app.common.util.SpiderHtmlUtils.java

/**
 * ?URLhtml?/*from w  ww . j  ava 2  s  .  c o  m*/
 * 
 * @param url
 * @return
 */
public static String getHtmlByUrl(String url) {
    if (StringUtils.isEmpty(url)) {
        return null;
    }
    String html = null;
    HttpClient httpClient = new DefaultHttpClient();// httpClient
    HttpUriRequest httpget = new HttpGet(url);// get?URL
    httpget.setHeader("Connection", "keep-alive");
    httpget.setHeader("Referer", "http://www.baidu.com");
    httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    httpget.setHeader("User-Agent",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36");
    try {
        HttpResponse responce = httpClient.execute(httpget);// responce
        int responseCode = responce.getStatusLine().getStatusCode();// ?
        if (responseCode == HttpStatus.SC_OK || responseCode == HttpStatus.SC_MOVED_PERMANENTLY
                || responseCode == HttpStatus.SC_MOVED_TEMPORARILY) {// 200 ?
            // 
            HttpEntity entity = responce.getEntity();
            if (entity != null) {
                html = EntityUtils.toString((org.apache.http.HttpEntity) entity);// html??
            }
        }
    } catch (Exception e) {
        logger.error("SpiderHtmlUtils:getHtmlByUrl sprider url={} error!!!", url);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
    return html;
}

From source file:com.beginner.core.utils.SmsUtil.java

/**
 * ????//from w ww  .jav  a 2 s  . c  o m
 * @param mobile ?
 * @param code  
 */
public static void sendSms2(String mobile, String code) {
    HttpUriRequest method = new HttpPost(Url);

    method.setHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");

    String strSMS2 = "";//2015/08/20 Tools.readTxtFile(Const.SMS2);         //?2?
    if (null != strSMS2 && !"".equals(strSMS2)) {
        String strS2[] = strSMS2.split(",beginner,");
        if (strS2.length == 2) {
        }
    }

    //      NameValuePair[] data = {//??
    //      new NameValuePair("account", account), new NameValuePair("password", password), //???32?MD5
    //            new NameValuePair("mobile", mobile), new NameValuePair("content", content), };
    //
    //      method.setRequestBody(data);

    //try {
    //client.executeMethod(method);

    //String SubmitResult = method.getResponseBodyAsString();

    //         Document doc = DocumentHelper.parseText(SubmitResult);
    //         Element root = doc.getRootElement();
    //
    //         code = root.elementText("code");
    //         String msg = root.elementText("msg");
    //         String smsid = root.elementText("smsid");
    //
    //         System.out.println(code);
    //         System.out.println(msg);
    //         System.out.println(smsid);
    //
    //         if (code == "2") {
    //            System.out.println("???");
    //         }

    //      } catch (HttpException e) {
    //         e.printStackTrace();
    //      } catch (IOException e) {
    //         e.printStackTrace();
    //      } catch (DocumentException e) {
    //         e.printStackTrace();
    //      }

}

From source file:ai.eve.volley.stack.HttpClientStack.java

private static void addHeaders(HttpUriRequest httpRequest, Map<String, String> headers) {
    for (String key : headers.keySet()) {
        httpRequest.setHeader(key, headers.get(key));
    }/*  w w w. j  a va  2s  .c  o m*/
}

From source file:com.vincestyling.netroid.stack.HttpClientStack.java

private static void addHeaders(HttpUriRequest httpRequest, Map<String, String> headers) {
    for (Entry<String, String> header : headers.entrySet()) {
        httpRequest.setHeader(header.getKey(), header.getValue());
    }//from  w w w .j  a  v a 2  s  .  c o  m
}

From source file:org.apache.vxquery.rest.AbstractRestServerTest.java

/**
 * Submit a {@link QueryRequest} and fetth the resulting
 * {@link AsyncQueryResponse}/*  ww  w.j  a va  2s.c om*/
 *
 * @param uri
 *            uri of the GET request
 * @param accepts
 *            application/json | application/xml
 * @param method
 *            Http Method to be used to send the request
 * @return Response received for the query request
 * @throws Exception
 */
protected static <T> T getQuerySuccessResponse(URI uri, String accepts, Class<T> type, String method)
        throws Exception {
    CloseableHttpClient httpClient = HttpClients.custom().setConnectionTimeToLive(20, TimeUnit.SECONDS).build();

    try {
        HttpUriRequest request = getRequest(uri, method);

        if (accepts != null) {
            request.setHeader(HttpHeaders.ACCEPT, accepts);
        }

        try (CloseableHttpResponse httpResponse = httpClient.execute(request)) {
            Assert.assertEquals(HttpResponseStatus.OK.code(), httpResponse.getStatusLine().getStatusCode());
            if (accepts != null) {
                Assert.assertEquals(accepts, httpResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
            }

            HttpEntity entity = httpResponse.getEntity();
            Assert.assertNotNull(entity);

            String response = RestUtils.readEntity(entity);
            return RestUtils.mapEntity(response, type, accepts);
        }
    } finally {
        HttpClientUtils.closeQuietly(httpClient);
    }
}

From source file:org.apache.vxquery.rest.AbstractRestServerTest.java

/**
 * Fetch the {@link QueryResultResponse} from query result endpoint once the
 * corresponding {@link QueryResultRequest} is given.
 *
 * @param resultRequest/* w w w. j a  v  a 2 s .co  m*/
 *            {@link QueryResultRequest}
 * @param accepts
 *            expected
 * 
 *            <pre>
 *            Accepts
 *            </pre>
 * 
 *            header in responses
 * @param method
 *            Http Method to be used to send the request
 * @return query result response received
 * @throws Exception
 */
protected static QueryResultResponse getQueryResultResponse(QueryResultRequest resultRequest, String accepts,
        String method) throws Exception {
    URI uri = RestUtils.buildQueryResultURI(resultRequest, restIpAddress, restPort);
    CloseableHttpClient httpClient = HttpClients.custom().setConnectionTimeToLive(20, TimeUnit.SECONDS).build();
    try {
        HttpUriRequest request = getRequest(uri, method);

        if (accepts != null) {
            request.setHeader(HttpHeaders.ACCEPT, accepts);
        }

        try (CloseableHttpResponse httpResponse = httpClient.execute(request)) {
            if (accepts != null) {
                Assert.assertEquals(accepts, httpResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
            }
            Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpResponseStatus.OK.code());

            HttpEntity entity = httpResponse.getEntity();
            Assert.assertNotNull(entity);

            String response = RestUtils.readEntity(entity);
            return RestUtils.mapEntity(response, QueryResultResponse.class, accepts);
        }
    } finally {
        HttpClientUtils.closeQuietly(httpClient);
    }
}

From source file:com.iflytek.android.framework.volley.toolbox.HttpClientStack.java

private static void addHeaders(HttpUriRequest httpRequest, Map<String, String> headers) {
    for (String key : headers.keySet()) {
        VolleyLog.d("2:" + key + ";" + headers.get(key));
        httpRequest.setHeader(key, headers.get(key));
    }/*from w  ww.  j ava2s. co  m*/
}

From source file:com.volley.air.toolbox.HttpClientStack.java

private static void addHeaders(HttpUriRequest httpRequest, Map<String, String> headers) {
    for (Map.Entry<String, String> entry : headers.entrySet()) {
        httpRequest.setHeader(entry.getKey(), entry.getValue());
    }//from   ww w .j av a2 s .  co  m
}

From source file:spark.protocol.SparqlCall.java

/**
 * Add headers to a request.//  w w w .  j  av a 2s  .com
 * @param req The request to set the headers on.
 */
static void addHeaders(HttpUriRequest req, String mimeType) {
    if (POST.equalsIgnoreCase(req.getMethod())) {
        req.addHeader(CONTENT_TYPE, FORM_ENCODED);
    }
    if (mimeType != null)
        req.setHeader(ACCEPT, mimeType);
}

From source file:com.android.volley.toolbox.http.HttpClientStack.java

private static void addHeaders(HttpUriRequest httpRequest, Map<String, String> headers) {
    if (headers != null) {
        for (String key : headers.keySet()) {
            httpRequest.setHeader(key, headers.get(key));
        }/*from  w w w.  j a v a  2  s  . c om*/
    }
}