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);
}
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;
}
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;
}
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);
}
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);
}
Aggregations