Search in sources :

Example 91 with HttpUriRequest

use of org.apache.http.client.methods.HttpUriRequest in project restfulie-java by caelum.

the class ApacheDispatcher method access.

private Response access(Request request, String method, URI uri) {
    HttpUriRequest verb = verbFor(method, uri);
    add(verb, request.getHeaders());
    return execute(request, verb);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest)

Example 92 with HttpUriRequest

use of org.apache.http.client.methods.HttpUriRequest in project opennms by OpenNMS.

the class HypericAckProcessor method fetchHypericAlerts.

/**
 * <p>fetchHypericAlerts</p>
 *
 * @param hypericUrl a {@link java.lang.String} object.
 * @param alertIds a {@link java.util.List} object.
 * @return a {@link java.util.List} object.
 * @throws org.apache.commons.httpclient.HttpException if any.
 * @throws java.io.IOException if any.
 * @throws javax.xml.bind.JAXBException if any.
 * @throws javax.xml.stream.XMLStreamException if any.
 */
public static List<HypericAlertStatus> fetchHypericAlerts(String hypericUrl, List<String> alertIds) throws IOException, JAXBException, XMLStreamException {
    List<HypericAlertStatus> retval = new ArrayList<HypericAlertStatus>();
    if (alertIds.size() < 1) {
        return retval;
    }
    for (int i = 0; i < alertIds.size(); i++) {
        // Construct the query string for the HTTP operation
        final StringBuilder alertIdString = new StringBuilder();
        alertIdString.append("?");
        for (int j = 0; (j < ALERTS_PER_HTTP_TRANSACTION) && (i < alertIds.size()); j++, i++) {
            if (j > 0)
                alertIdString.append("&");
            // Numeric values, no need to worry about URL encoding
            alertIdString.append("id=").append(alertIds.get(i));
        }
        final HttpClientWrapper clientWrapper = HttpClientWrapper.create().setConnectionTimeout(3000).setSocketTimeout(3000).setUserAgent("OpenNMS-Ackd.HypericAckProcessor");
        HttpUriRequest httpMethod = new HttpGet(hypericUrl + alertIdString.toString());
        // Parse the URI from the config so that we can deduce the username/password information
        String userinfo = null;
        try {
            URI hypericUri = new URI(hypericUrl);
            userinfo = hypericUri.getUserInfo();
        } catch (final URISyntaxException e) {
            LOG.warn("Could not parse URI to get username/password stanza: {}", hypericUrl, e);
        }
        if (userinfo != null && !"".equals(userinfo)) {
            final String[] credentials = userinfo.split(":");
            if (credentials.length == 2) {
                clientWrapper.addBasicCredentials(credentials[0], credentials[1]).usePreemptiveAuth();
            } else {
                LOG.warn("Unable to deduce username/password from '{}'", userinfo);
            }
        }
        try {
            CloseableHttpResponse response = clientWrapper.execute(httpMethod);
            retval = parseHypericAlerts(new StringReader(EntityUtils.toString(response.getEntity())));
        } finally {
            IOUtils.closeQuietly(clientWrapper);
        }
    }
    return retval;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HttpClientWrapper(org.opennms.core.web.HttpClientWrapper) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) StringReader(java.io.StringReader)

Example 93 with HttpUriRequest

use of org.apache.http.client.methods.HttpUriRequest in project SmartAndroidSource by jaychou2012.

the class RetryHandler method retryRequest.

@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    boolean retry = true;
    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b);
    if (executionCount > maxRetries) {
        // Do not retry if over max retry count
        retry = false;
    } else if (isInList(exceptionWhitelist, exception)) {
        // immediately retry if error is whitelisted
        retry = true;
    } else if (isInList(exceptionBlacklist, exception)) {
        // immediately cancel retry if the error is blacklisted
        retry = false;
    } else if (!sent) {
        // for most other errors, retry only if request hasn't been fully sent yet
        retry = true;
    }
    if (retry) {
        // resend all idempotent requests
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        if (currentReq == null) {
            return false;
        }
    }
    if (retry) {
        SystemClock.sleep(retrySleepTimeMS);
    } else {
        exception.printStackTrace();
    }
    return retry;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest)

Example 94 with HttpUriRequest

use of org.apache.http.client.methods.HttpUriRequest in project FastDev4Android by jiangqqlmj.

the class HttpClientStack method performRequest.

/**
     * 请求执行
     * @param request the request to perform
     * @param additionalHeaders additional headers to be sent together with
     *         {@link Request#getHeaders()}
     * @return
     * @throws IOException
     * @throws AuthFailureError
     */
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
    //传入request 进行创建封装过后的httprequest子类 httpurlrequest
    HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
    addHeaders(httpRequest, additionalHeaders);
    addHeaders(httpRequest, request.getHeaders());
    onPrepareRequest(httpRequest);
    HttpParams httpParams = httpRequest.getParams();
    int timeoutMs = request.getTimeoutMs();
    // TODO: Reevaluate this connection timeout based on more wide-scale
    // data collection and possibly different for wifi vs. 3G.
    HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
    HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
    return mClient.execute(httpRequest);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpParams(org.apache.http.params.HttpParams)

Example 95 with HttpUriRequest

use of org.apache.http.client.methods.HttpUriRequest in project FastDev4Android by jiangqqlmj.

the class HttpClientStackTest method createPostRequest.

@Test
public void createPostRequest() throws Exception {
    TestRequest.Post request = new TestRequest.Post();
    assertEquals(request.getMethod(), Method.POST);
    HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
    assertTrue(httpRequest instanceof HttpPost);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) HttpPost(org.apache.http.client.methods.HttpPost) TestRequest(com.android.volley.mock.TestRequest) Test(org.junit.Test)

Aggregations

HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)185 Test (org.junit.Test)62 TestRequest (com.android.volley.mock.TestRequest)52 HttpGet (org.apache.http.client.methods.HttpGet)45 URI (java.net.URI)43 HttpResponse (org.apache.http.HttpResponse)41 HttpEntity (org.apache.http.HttpEntity)38 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)36 HttpPost (org.apache.http.client.methods.HttpPost)22 IOException (java.io.IOException)19 Header (org.apache.http.Header)18 JSONObject (org.json.JSONObject)18 BufferedReader (java.io.BufferedReader)17 InputStreamReader (java.io.InputStreamReader)17 PrintWriter (java.io.PrintWriter)17 StringWriter (java.io.StringWriter)17 HttpHost (org.apache.http.HttpHost)13 HttpPut (org.apache.http.client.methods.HttpPut)12 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)11 StringEntity (org.apache.http.entity.StringEntity)10