下面列出了org.apache.http.impl.client.DefaultHttpClient 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean start() {
try {
httpClient = new DefaultHttpClient(new PoolingClientConnectionManager());
bulkUri = makeURI(elasticSearchBaseUrl, "_bulk");
/* only for debugging */
if (deleteAllIndexWhenStart) {
try {
deleteIndex(null);
} catch (Exception ex) {
logger.warn(String.format("Failed to delete all index"), ex);
}
}
populateExtensions();
populateTriggerVOs();
populateInventoryIndexer();
dumpInventoryIndexer();
createIndexIfNotExists();
bus.registerService(this);
} catch (Exception e) {
throw new CloudRuntimeException(e);
}
return true;
}
public static GridInfo getHostNameAndPort(String hubHost, int hubPort, SessionId session) {
GridInfo retVal = null;
try {
HttpHost host = new HttpHost(hubHost, hubPort);
DefaultHttpClient client = new DefaultHttpClient();
URL sessionURL = new URL("http://" + hubHost + ":" + hubPort + "/grid/api/testsession?session=" + session);
BasicHttpEntityEnclosingRequest basicHttpEntityEnclosingRequest = new BasicHttpEntityEnclosingRequest("POST", sessionURL.toExternalForm());
HttpResponse response = client.execute(host, basicHttpEntityEnclosingRequest);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
JSONObject object = extractObject(response);
retVal = new GridInfo(object);
} else {
System.out.println("Problem connecting to Grid Server");
}
} catch (JSONException | IOException e) {
throw new RuntimeException("Failed to acquire remote webdriver node and port info", e);
}
return retVal;
}
/**
* Makes a GET request to the given address. Any query string should be appended already.
* @param address the fully qualified URL to make the request to
* @return
*/
private String doGet(String address){
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(address);
HttpResponse response = httpclient.execute(httpget);
//check reponse code
StatusLine status = response.getStatusLine();
if(status.getStatusCode() != 200) {
log.error("Error shortening URL. Status: " + status.getStatusCode() + ", reason: " + status.getReasonPhrase());
return null;
}
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity);
}
} catch (Exception e) {
log.error(e.getClass() + ":" + e.getMessage());
}
return null;
}
private boolean isExternalEndpointAvailable() throws IOException {
HttpClient httpClient = new DefaultHttpClient();
String url = "http://semantic.eea.europa.eu/sparql?query=";
String query = "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>\n"
+ "PREFIX cr:<http://cr.eionet.europa.eu/ontologies/contreg.rdf#>\n"
+ "SELECT * WHERE { ?bookmark a cr:SparqlBookmark;rdfs:label ?label} LIMIT 50";
url = url + URLEncoder.encode(query, "UTF-8");
HttpGet httpGet = new HttpGet(url);
httpClient.getParams().setParameter("http.socket.timeout", 300000);
httpGet.setHeader("Accept", "text/xml");
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
return true;
}
return false;
}
private void invokeBootstrapExtension() throws ClientProtocolException, IOException {
final String params = Common.BALANCED ? "?rs%3Abalanced=true" : "";
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(host, port),
new UsernamePasswordCredentials(username, password));
HttpPost post = new HttpPost("http://" + host + ":" + port
+ "/v1/resources/bootstrap" + params);
HttpResponse response = client.execute(post);
@SuppressWarnings("unused")
HttpEntity entity = response.getEntity();
System.out.println("Invoked bootstrap extension. Response is "
+ response.toString());
}
@Test
public void testRestEndPoints() throws Exception {
HttpClient client = new DefaultHttpClient();
for (Map.Entry<String, String> restEndPoint : REST_END_POINTS.entrySet()) {
final String endPoint = restEndPoint.getKey();
LOG.info("REST endpoint " + endPoint);
HttpGet restGet = new HttpGet(endPoint);
HttpResponse response = client.execute(restGet);
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals(restEndPoint.getValue(), response.getEntity().getContentType().getValue());
// need to consume full response before make another rest call with
// the default SingleClientConnManager used with DefaultHttpClient
EntityUtils.consume(response.getEntity());
}
}
public static String GET(String url){
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
private boolean isRunning(String host) {
try {
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(new AuthScope(host, 7997),
new UsernamePasswordCredentials("admin", "admin"));
HttpGet get = new HttpGet("http://" + host + ":7997?format=json");
HttpResponse response = client.execute(get);
ResponseHandler<String> handler = new BasicResponseHandler();
String body = handler.handleResponse(response);
if (body.contains("Healthy")) {
return true;
}
} catch (Exception e) {
return false;
}
return false;
}
public HttpClient createHttpClient(ClientConfiguration clientconfiguration)
{
BasicHttpParams basichttpparams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(basichttpparams, clientconfiguration.getConnectionTimeout());
HttpConnectionParams.setSoTimeout(basichttpparams, clientconfiguration.getSocketTimeout());
HttpConnectionParams.setStaleCheckingEnabled(basichttpparams, true);
HttpConnectionParams.setTcpNoDelay(basichttpparams, true);
int i = clientconfiguration.getSocketBufferSizeHints()[0];
int j = clientconfiguration.getSocketBufferSizeHints()[1];
if (i > 0 || j > 0)
{
HttpConnectionParams.setSocketBufferSize(basichttpparams, Math.max(i, j));
}
SchemeRegistry schemeregistry = new SchemeRegistry();
schemeregistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
if (clientconfiguration.getProtocol() == Protocol.HTTPS)
{
schemeregistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
}
return new DefaultHttpClient(new SingleClientConnManager(basichttpparams, schemeregistry), basichttpparams);
}
static String makeRequest(String uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
} else {
//Close the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (IOException e) {
e.printStackTrace();
}
return responseString;
}
public static String GET(String url){
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
public String readJson(String url) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
}
} catch(Exception e) {
e.printStackTrace();
}
return builder.toString();
}
@Test(groups = "wso2.esb", description = "Test CorrelateOn in Aggregate mediator ")
public void testAggregateWithCorrelateExpression() throws IOException{
String expectedOutput1 = "<result><value>value1</value><value>value2</value></result>";
String expectedOutput2 = "<result><value>value2</value><value>value1</value></result>";
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(getApiInvocationURL("testAggregate"));
try {
HttpResponse httpResponse = httpclient.execute(httpget);
HttpEntity entity = httpResponse.getEntity();
BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
String result = "";
String line;
while ((line = rd.readLine()) != null) {
result += line;
}
Assert.assertTrue(expectedOutput1.equals(result) || expectedOutput2.equals(result), "Aggregated response is not correct.");
}
finally {
httpclient.clearRequestInterceptors();
}
}
public static String httpPostWithWCF(String serverUrl, String method,
JSONObject params) {
String responseStr = null;
try {
String url = serverUrl + method;
DefaultHttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
request.setEntity(new StringEntity(params.toString(), CODE));
request.setHeader(HTTP.CONTENT_TYPE, "text/json");
HttpResponse response = client.execute(request);
responseStr = EntityUtils.toString(response.getEntity(), CODE);
// System.out.println("responseStr:" + responseStr);
} catch (Exception e) {
e.printStackTrace();
}
return responseStr;
}
public static String get(String URL) throws Exception {
// Get custom http client
DefaultHttpClient client = getHTTPClient();
// Create GET request
HttpGet request = new HttpGet(URL);
// Execute the request
HttpResponse response = client.execute(request, new BasicHttpContext());
// Return response as string
String responseText = EntityUtils.toString(response.getEntity());
// Failed?
if (response.getStatusLine() == null || response.getStatusLine().getStatusCode() != 200) {
// Throw it out
throw new Exception(response.getStatusLine().toString() + "\n" + responseText);
}
// We're good
return responseText;
}
public static String getConcept(JSONObject jjj)
throws UnsupportedEncodingException, IOException, ClientProtocolException {
HttpPost httppost = new HttpPost(vocubularyurl);
// httppost.setHeader("X-GWT-Permutation",
// "3DE824138FE65400740EC1816A73CACC");
httppost.setHeader("Content-Type", "application/json");
StringEntity se = new StringEntity(jjj.toString());
httppost.setEntity(se);
startTime = System.currentTimeMillis();
HttpResponse httpresponse = new DefaultHttpClient().execute(httppost);
endTime = System.currentTimeMillis();
System.out.println("statusCode:" + httpresponse.getStatusLine().getStatusCode());
System.out.println("Call API time (unit:millisecond):" + (endTime - startTime));
if (httpresponse.getStatusLine().getStatusCode() == 200) {
// System.out.println("succeed!");
String strResult = EntityUtils.toString(httpresponse.getEntity());
return strResult;
// httppost.
} else {
return null;
}
}
/**
* Create an {@link ApacheHttpTransport} for calling Google APIs with an optional HTTP proxy.
*
* @param proxyUri Optional HTTP proxy URI to use with the transport.
* @param proxyCredentials Optional HTTP proxy credentials to authenticate with the transport
* proxy.
* @return The resulting HttpTransport.
* @throws IOException If there is an issue connecting to Google's certification server.
* @throws GeneralSecurityException If there is a security issue with the keystore.
*/
public static ApacheHttpTransport createApacheHttpTransport(
@Nullable URI proxyUri, @Nullable Credentials proxyCredentials)
throws IOException, GeneralSecurityException {
checkArgument(
proxyUri != null || proxyCredentials == null,
"if proxyUri is null than proxyCredentials should be null too");
ApacheHttpTransport transport =
new ApacheHttpTransport.Builder()
.trustCertificates(GoogleUtils.getCertificateTrustStore())
.setProxy(
proxyUri == null ? null : new HttpHost(proxyUri.getHost(), proxyUri.getPort()))
.build();
if (proxyCredentials != null) {
((DefaultHttpClient) transport.getHttpClient())
.getCredentialsProvider()
.setCredentials(new AuthScope(proxyUri.getHost(), proxyUri.getPort()), proxyCredentials);
}
return transport;
}
public static void reqArrayNoSSL(String url, Context parent,
ModelCallback<JSONArray> callback) {
RequestAsyncTask<JSONArray> task = new RequestAsyncTask<JSONArray>(
"get", url, null, false, false, parent, callback) {
@Override
protected JSONArray toJSON(String responseStr) throws JSONException {
return new JSONArray(responseStr);
}
};
task.client = new DefaultHttpClient();
task.client.getParams().setParameter(
ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
task.headers = new HashMap<String, String>();
task.headers.put("User-Agent", BROWSER_LIKE_USER_AGENT);
executeOnExecutor(task, HIGH_PRIORITY_EXECUTOR);
}
public String getToken() {
Log.d(TAG, "attempting to get a token from: " + m_strTokenFactoryURL);
try {
// DISCLAIMER: the application developer should implement an authentication mechanism from the mobile app to the
// server side app so the token factory in the server only provides tokens to authenticated clients
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(m_strTokenFactoryURL);
HttpResponse executed = httpClient.execute(httpGet);
InputStream is = executed.getEntity().getContent();
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, "UTF-8");
String strToken = writer.toString();
Log.d(TAG, strToken);
return strToken;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
public void deleteWorkspace(Workspace workspace) throws Exception {
if (!isTokenActive()) {
login();
}
HttpClient httpClient = new DefaultHttpClient();
String url = this.storageUrl + "/" + workspace.getSwiftContainer();
try {
HttpDelete request = new HttpDelete(url);
request.setHeader(SwiftResponse.X_AUTH_TOKEN, authToken);
HttpResponse response = httpClient.execute(request);
SwiftResponse swiftResponse = new SwiftResponse(response);
if (swiftResponse.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
throw new UnauthorizedException("401 User unauthorized");
}
if (swiftResponse.getStatusCode() < 200 || swiftResponse.getStatusCode() >= 300) {
throw new UnexpectedStatusCodeException("Unexpected status code: " + swiftResponse.getStatusCode());
}
} finally {
httpClient.getConnectionManager().shutdown();
}
}
public static void sendReplaceResult(String url, String key, String secret, String sourcedid, String score, String resultData, Boolean isUrl) throws IOException, OAuthException, GeneralSecurityException {
HttpPost request = buildReplaceResult(url, key, secret, sourcedid, score, resultData, isUrl);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() >= 400) {
throw new HttpResponseException(response.getStatusLine().getStatusCode(),
response.getStatusLine().getReasonPhrase());
}
}
public void sendViaSendcloudApi(EmailParams emailParams) throws IOException {
String url = jHipsterProperties.getSendcloud().getUrl();
String key = jHipsterProperties.getSendcloud().getKey();
String user = jHipsterProperties.getSendcloud().getUser();
String from = jHipsterProperties.getSendcloud().getFrom();
String fromName = jHipsterProperties.getSendcloud().getFromName();
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("api_user", user));
params.add(new BasicNameValuePair("api_key", key));
params.add(new BasicNameValuePair("to", emailParams.getTo()));
params.add(new BasicNameValuePair("from", from));
params.add(new BasicNameValuePair("fromname", fromName));
params.add(new BasicNameValuePair("subject", emailParams.getSubject()));
params.add(new BasicNameValuePair("html", emailParams.getContent()));
params.add(new BasicNameValuePair("resp_email_id", "true"));
HttpPost httpost = new HttpPost(url);
HttpClient httpclient = new DefaultHttpClient();
httpost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httpost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
log.debug("Send email via Sendcloud successfully. response: " + EntityUtils.toString(response.getEntity()));
} else {
log.error("Send email via Sendcloud failed.");
}
httpost.releaseConnection();
}
public static void loginBox() throws Exception {
HttpParams params = new BasicHttpParams();
params.setParameter(
"http.useragent",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6");
DefaultHttpClient httpclient = new DefaultHttpClient(params);
//https://www.box.net/api/1.0/auth/jtear5a2djtfxs9598apynmea62vko5d
System.out.println("Trying to log in to box.com");
HttpPost httppost = new HttpPost("https://www.box.net/api/1.0/auth/" + ticket);
httppost.setHeader("Cookie", zcookie + ";" + visitorcookie);
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
// formparams.add(new BasicNameValuePair("action", "login"));
formparams.add(new BasicNameValuePair("login", "[email protected]"));
formparams.add(new BasicNameValuePair("password", ""));
formparams.add(new BasicNameValuePair("__login", "1"));
formparams.add(new BasicNameValuePair("dologin", "1"));
formparams.add(new BasicNameValuePair("reg_step", ""));
formparams.add(new BasicNameValuePair("submit1", "1"));
formparams.add(new BasicNameValuePair("folder", ""));
formparams.add(new BasicNameValuePair("skip_framework_login", "1"));
formparams.add(new BasicNameValuePair("login_or_register_mode", "login"));
formparams.add(new BasicNameValuePair("new_login_or_register_mode", ""));
formparams.add(new BasicNameValuePair("request_token", request_token));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(entity);
HttpResponse httpresponse = httpclient.execute(httppost);
System.out.println("Gonna print the response");
loginresponse = EntityUtils.toString(httpresponse.getEntity());
if (loginresponse.contains("Invalid username or password")) {
System.out.println("DropBox login failed");
} else {
System.out.println("DropbBox login successful :)");
}
}
public static void createRESTAppServer(String restServerName, int restPort) {
try {
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope("localhost", 8002),
new UsernamePasswordCredentials("admin", "admin"));
HttpPost post = new HttpPost("http://localhost:8002" + "/v1/rest-apis?format=json");
String JSONString =
"{ \"rest-api\": {\"name\":\"" +
restServerName +
"\",\"port\":\"" +
restPort +
"\"}}";
// System.out.println(JSONString);
post.addHeader("Content-type", "application/json");
post.setEntity(new StringEntity(JSONString));
HttpResponse response = client.execute(post);
HttpEntity respEntity = response.getEntity();
if (respEntity != null) {
// EntityUtils to get the response content
String content = EntityUtils.toString(respEntity);
System.out.println(content);
}
} catch (Exception e) {
// writing error to Log
e.printStackTrace();
}
}
public void setup() {
DefaultHttpClient defaultClient = new DefaultHttpClient(new PoolingClientConnectionManager(schemeRegistry), params);
auth.setupConnection(defaultClient);
if (enableGZip) {
underlying.set(new DecompressingHttpClient(defaultClient));
} else {
underlying.set(defaultClient);
}
}
public static InputStream getInputStreamFromUrl(String url)
throws ClientProtocolException, IOException {
HttpGet request = new HttpGet(url);
DefaultHttpClient client = new DefaultHttpClient(getTimeoutHttpParams());
HttpResponse response = client.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
Log.e(TAG, "Failed to get stream for: " + url);
throw new IOException("Failed to get stream for: " + url);
}
return response.getEntity().getContent();
}
private HttpClient getHttpClient(int connectionTimeout, int readTimeout) {
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(httpParams, readTimeout);
HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeout);
return new DefaultHttpClient(httpParams);
}
private void configureCredentials(DefaultHttpClient httpClient, PasswordCredentials credentials) {
String username = credentials.getUsername();
if (username != null && username.length() > 0) {
useCredentials(httpClient, credentials, AuthScope.ANY_HOST, AuthScope.ANY_PORT);
// Use preemptive authorisation if no other authorisation has been established
httpClient.addRequestInterceptor(new PreemptiveAuth(new BasicScheme()), 0);
}
}
/**
* Returns HTTP GET response from given url using admin credentials
*
* @param url request url suffix
* @return
* @throws IOException
*/
public static HttpResponse getRequestResponse(String url) throws IOException {
String restUrl = getRestEndPoint(url);
log.info("Sending HTTP GET request: " + restUrl);
client = new DefaultHttpClient();
request = new HttpGet(restUrl);
request.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials("admin", "admin"),
Charset.defaultCharset().toString(), false));
client.getConnectionManager().closeExpiredConnections();
HttpResponse response = client.execute(request);
return response;
}
private static int sendDELETE(String endpoint, String acceptType) throws IOException {
HttpClient httpClient = new DefaultHttpClient();
HttpDelete httpDelete = new HttpDelete(endpoint);
httpDelete.setHeader("Accept", acceptType);
HttpResponse httpResponse = httpClient.execute(httpDelete);
return httpResponse.getStatusLine().getStatusCode();
}