当前位置: 首页>>代码示例>>Java>>正文


Java HttpClient.execute方法代码示例

本文整理汇总了Java中org.apache.http.client.HttpClient.execute方法的典型用法代码示例。如果您正苦于以下问题:Java HttpClient.execute方法的具体用法?Java HttpClient.execute怎么用?Java HttpClient.execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.http.client.HttpClient的用法示例。


在下文中一共展示了HttpClient.execute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: doPost

import org.apache.http.client.HttpClient; //导入方法依赖的package包/类
/**
 * Post String
 *
 * @param host
 * @param path
 * @param method
 * @param headers
 * @param querys
 * @param body
 * @return
 * @throws Exception
 */
public static HttpResponse doPost(String host, String path, String method,
                                  Map<String, String> headers,
                                  Map<String, String> querys,
                                  String body)
        throws Exception {
    HttpClient httpClient = wrapClient(host);

    HttpPost request = new HttpPost(buildUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
        request.addHeader(e.getKey(), e.getValue());
    }

    if (StringUtils.isNotBlank(body)) {
        request.setEntity(new StringEntity(body, "utf-8"));
    }

    return httpClient.execute(request);
}
 
开发者ID:JiaXiaohei,项目名称:elasticjob-stock-push,代码行数:31,代码来源:HttpUtils.java

示例2: doPost

import org.apache.http.client.HttpClient; //导入方法依赖的package包/类
/**
 * Post String
 * 
 * @param host
 * @param path
 * @param method
 * @param headers
 * @param querys
 * @param body
 * @return
 * @throws Exception
 */
public static HttpResponse doPost(String host, String path, String method, 
		Map<String, String> headers, 
		Map<String, String> querys, 
		String body)
           throws Exception {    	
   	HttpClient httpClient = wrapClient(host);

   	HttpPost request = new HttpPost(buildUrl(host, path, querys));
       for (Map.Entry<String, String> e : headers.entrySet()) {
       	request.addHeader(e.getKey(), e.getValue());
       }

       if (StringUtils.isNotBlank(body)) {
       	request.setEntity(new StringEntity(body, "utf-8"));
       }

       return httpClient.execute(request);
   }
 
开发者ID:linkingli,项目名称:FaceDistinguish,代码行数:31,代码来源:HttpUtils.java

示例3: testPropagationAfterRedirect

import org.apache.http.client.HttpClient; //导入方法依赖的package包/类
@Test
public void testPropagationAfterRedirect() throws IOException {
    {
        HttpClient client = clientBuilder.build();
        client.execute(new HttpGet(serverUrl(RedirectHandler.MAPPING)));
    }

    List<MockSpan> mockSpans = mockTracer.finishedSpans();
    Assert.assertEquals(3, mockSpans.size());

    // the last one is for redirect
    MockSpan mockSpan = mockSpans.get(1);
    Assert.assertEquals(PropagationHandler.lastRequest.getFirstHeader("traceId").getValue(),
            String.valueOf(mockSpan.context().traceId()));
    Assert.assertEquals(PropagationHandler.lastRequest.getFirstHeader("spanId").getValue(),
            String.valueOf(mockSpan.context().spanId()));

    assertLocalSpan(mockSpans.get(2));
}
 
开发者ID:opentracing-contrib,项目名称:java-apache-httpclient,代码行数:20,代码来源:TracingHttpClientBuilderTest.java

示例4: useHttpClientPost

import org.apache.http.client.HttpClient; //导入方法依赖的package包/类
private void useHttpClientPost(String url) {
    HttpPost mHttpPost = new HttpPost(url);
    mHttpPost.addHeader("Connection", "Keep-Alive");
    try {
        HttpClient mHttpClient = createHttpClient();
        List<NameValuePair> postParams = new ArrayList<>();
        //要传递的参数
        postParams.add(new BasicNameValuePair("ip", "59.108.54.37"));
        mHttpPost.setEntity(new UrlEncodedFormEntity(postParams));
        HttpResponse mHttpResponse = mHttpClient.execute(mHttpPost);
        HttpEntity mHttpEntity = mHttpResponse.getEntity();
        int code = mHttpResponse.getStatusLine().getStatusCode();
        if (null != mHttpEntity) {
            InputStream mInputStream = mHttpEntity.getContent();
            String respose = converStreamToString(mInputStream);
            Log.d(TAG, "请求状态码:" + code + "\n请求结果:\n" + respose);
            mInputStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
开发者ID:henrymorgen,项目名称:android-advanced-light,代码行数:23,代码来源:MainActivity.java

示例5: doPut

import org.apache.http.client.HttpClient; //导入方法依赖的package包/类
/**
 * Put stream
 *
 * @param host
 * @param path
 * @param method
 * @param headers
 * @param querys
 * @param body
 * @return
 * @throws Exception
 */
public static HttpResponse doPut(String host, String path, String method,
                                 Map<String, String> headers,
                                 Map<String, String> querys,
                                 byte[] body)
        throws Exception {
    HttpClient httpClient = wrapClient(host);

    HttpPut request = new HttpPut(buildUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
        request.addHeader(e.getKey(), e.getValue());
    }

    if (body != null) {
        request.setEntity(new ByteArrayEntity(body));
    }

    return httpClient.execute(request);
}
 
开发者ID:JiaXiaohei,项目名称:elasticjob-oray-client,代码行数:31,代码来源:HttpUtils.java

示例6: getTestNetApiJsonAtUrl

import org.apache.http.client.HttpClient; //导入方法依赖的package包/类
/**
 * return the testnet URL with the given suffix.
 *
 * @param urlSuffix
 *            the url suffix to use.
 * @return the testnet URL with the given suffix.
 */
private static JSONObject getTestNetApiJsonAtUrl(final String urlSuffix) {
	try {
		final HttpGet get = new HttpGet(TESTNET_API + urlSuffix);
		final HttpClient client = getHttpClient();
		final HttpResponse response = client.execute(get);
		LOG.debug("test net status:{}", response.getStatusLine());
		final HttpEntity entity = response.getEntity();
		final String entityStr = EntityUtils.toString(entity);
		LOG.debug("test net entityStr:{}", entityStr);
		final JSONObject json = new JSONObject(entityStr);
		return json;
	} catch (final IOException e) {
		throw new RuntimeException(e);
	}
}
 
开发者ID:coranos,项目名称:neo-java,代码行数:23,代码来源:CityOfZionUtil.java

示例7: doPost

import org.apache.http.client.HttpClient; //导入方法依赖的package包/类
/**
 * Post stream
 *
 * @param host
 * @param path
 * @param method
 * @param headers
 * @param querys
 * @param body
 * @return
 * @throws Exception
 */
public static HttpResponse doPost(String host, String path, String method,
                                  Map<String, String> headers,
                                  Map<String, String> querys,
                                  byte[] body)
        throws Exception {
    HttpClient httpClient = wrapClient(host);

    HttpPost request = new HttpPost(buildUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
        request.addHeader(e.getKey(), e.getValue());
    }

    if (body != null) {
        request.setEntity(new ByteArrayEntity(body));
    }

    return httpClient.execute(request);
}
 
开发者ID:noesblog,项目名称:aliyunOcrSDK,代码行数:31,代码来源:HttpUtils.java

示例8: doPut

import org.apache.http.client.HttpClient; //导入方法依赖的package包/类
/**
 * Put stream
 * @param host
 * @param path
 * @param method
 * @param headers
 * @param querys
 * @param body
 * @return
 * @throws Exception
 */
public static HttpResponse doPut(String host, String path, String method, 
		Map<String, String> headers, 
		Map<String, String> querys, 
		byte[] body)
           throws Exception {    	
   	HttpClient httpClient = wrapClient(host);

   	HttpPut request = new HttpPut(buildUrl(host, path, querys));
       for (Map.Entry<String, String> e : headers.entrySet()) {
       	request.addHeader(e.getKey(), e.getValue());
       }

       if (body != null) {
       	request.setEntity(new ByteArrayEntity(body));
       }

       return httpClient.execute(request);
   }
 
开发者ID:linkingli,项目名称:FaceDistinguish,代码行数:30,代码来源:HttpUtils.java

示例9: doPut

import org.apache.http.client.HttpClient; //导入方法依赖的package包/类
/**
 * Put String
 * @param host
 * @param path
 * @param method
 * @param headers
 * @param querys
 * @param body
 * @return
 * @throws Exception
 */
public static HttpResponse doPut(String host, String path, String method,
                                 Map<String, String> headers,
                                 Map<String, String> querys,
                                 String body)
        throws Exception {
    HttpClient httpClient = wrapClient(host);

    HttpPut request = new HttpPut(buildUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
        request.addHeader(e.getKey(), e.getValue());
    }

    if (StringUtils.isNotBlank(body)) {
        request.setEntity(new StringEntity(body, "utf-8"));
    }

    return httpClient.execute(request);
}
 
开发者ID:noesblog,项目名称:aliyunOcrSDK,代码行数:30,代码来源:HttpUtils.java

示例10: getNewOnions

import org.apache.http.client.HttpClient; //导入方法依赖的package包/类
public static synchronized Object[] getNewOnions() {
    Vector<Object> out = new Vector<>();

    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(URLGenerate);

    // add request header
    request.addHeader("User-Agent", "OnionHarvester - Java Client");
    try {
        HttpResponse response = client.execute(request);

        if (response.getStatusLine().getStatusCode() != 200) {
            out.add(false);
            return out.toArray();
        }

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }

        String temp = result.toString();
        jobj = new JSONObject(temp);
        jobj.getJSONArray("ports").iterator().forEachRemaining(o -> {
            getPorts().add(Integer.valueOf((String) o));
        });
        out.add(true);
        out.add(jobj.getString("start"));
        out.add(jobj.getString("end"));
        out.add(jobj.getString("id"));
    } catch (Exception ex) {
        out.add(false);
    } finally {
        return out.toArray();
    }
}
 
开发者ID:mirsamantajbakhsh,项目名称:OnionHarvester,代码行数:40,代码来源:OH.java

示例11: isRaisingTrend

import org.apache.http.client.HttpClient; //导入方法依赖的package包/类
public boolean isRaisingTrend() {
	double aggregate = 0;
	double SMA100;
	HttpClientBuilder hcb = HttpClientBuilder.create();
	HttpClient client = hcb.build();
	HttpGet request = new HttpGet(RequestURI.baseURL+"/v1/candles?instrument=EUR_USD&count=100&granularity=M30&candleFormat=midpoint");
	request.addHeader(RequestURI.headerTitle,RequestURI.accessToken);
	try {
		HttpResponse response = client.execute(request);
		BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		StringBuffer result = new StringBuffer();
		String line = "";
		while ((line = rd.readLine()) != null) {
			result.append(line);
		}
		JSONObject resultJson = new JSONObject(result.toString());
		JSONArray candles = resultJson.getJSONArray("candles");
		for (int i=0; i<candles.length(); i++) {
			double closePrice = Double.parseDouble(candles.getJSONObject(i).get("closeMid").toString());
			aggregate += closePrice;
		}
		SMA100 = Trading.round(aggregate/100, 5);
		if (getLatestCandle().getClose() > SMA100) return true;
		else return false;
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return false;
}
 
开发者ID:toni8810,项目名称:TradingRobot,代码行数:31,代码来源:Candle.java

示例12: readyPageReturns200andOK

import org.apache.http.client.HttpClient; //导入方法依赖的package包/类
@Test
public void readyPageReturns200andOK() throws Exception {
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpResponse response = httpClient.execute(new HttpGet("http://localhost:7001/ready"));

    assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);

    assertThat(responseBody(response)).contains("OK");
}
 
开发者ID:tjheslin1,项目名称:Patterdale,代码行数:10,代码来源:PatterdaleTest.java

示例13: postSlackCommandWithFile

import org.apache.http.client.HttpClient; //导入方法依赖的package包/类
private void postSlackCommandWithFile(Map<String, String> params, byte [] fileContent, String fileName, String command, SlackMessageHandleImpl handle) {
    URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.setScheme(SLACK_API_SCHEME).setHost(SLACK_API_HOST).setPath(SLACK_API_PATH+"/"+command);
    for (Map.Entry<String, String> arg : params.entrySet())
    {
        uriBuilder.setParameter(arg.getKey(),arg.getValue());
    }
    HttpPost request = new HttpPost(uriBuilder.toString());
    HttpClient client = getHttpClient();
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    try
    {
        builder.addBinaryBody("file",fileContent, ContentType.DEFAULT_BINARY,fileName);
        request.setEntity(builder.build());
        HttpResponse response = client.execute(request);
        String jsonResponse = ReaderUtils.readAll(new InputStreamReader(response.getEntity().getContent()));
        LOGGER.debug("PostMessage return: " + jsonResponse);
        ParsedSlackReply reply = SlackJSONReplyParser.decode(parseObject(jsonResponse),this);
        handle.setReply(reply);
    }
    catch (Exception e)
    {
        // TODO : improve exception handling
        e.printStackTrace();
    }
}
 
开发者ID:riversun,项目名称:slacklet,代码行数:27,代码来源:SlackWebSocketSessionImpl.java

示例14: getUrl

import org.apache.http.client.HttpClient; //导入方法依赖的package包/类
public static String getUrl(String url, List<NameValuePair> params) {
    String uri = url;
    if (params != null) uri += URLEncodedUtils.format(params, "UTF-8");
    //System.out.println(uri.toString());
    log.info(uri.toString());

    HttpGet httpget = new HttpGet(uri);
    HttpClient httpclient = new DefaultHttpClient();

    httpclient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET, "UTF-8");
    //请求超时 ,连接超时
    httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);
    //读取超时
    httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT);

    String rs = "";

    try {
        HttpResponse response = httpclient.execute(httpget);//  httpClient.executeMethod(postMethod);

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            rs = EntityUtils.toString(response.getEntity(), "UTF-8");


            log.info("rs: " + rs);

            return rs;
        } else {
            System.out.println("rs: not HttpStatus.SC_OK");
        }
    } catch (Exception e) {
        e.printStackTrace();

    } finally {
        httpclient.getConnectionManager().shutdown();
    }
    return rs;
}
 
开发者ID:Yunfeng,项目名称:weixinpay,代码行数:39,代码来源:HttpUtil.java

示例15: launchRequest

import org.apache.http.client.HttpClient; //导入方法依赖的package包/类
public void launchRequest(final SipProfile acc) {
    Thread t = new Thread() {

        public void run() {
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpRequestBase req = getRequest(acc);
                if(req == null) {
                    return;
                }
                // Create a response handler
                HttpResponse httpResponse = httpClient.execute(req);
                if(httpResponse.getStatusLine().getStatusCode() == 200) {
                    InputStreamReader isr = new InputStreamReader(httpResponse.getEntity().getContent());
                    BufferedReader br = new BufferedReader(isr);

                    String line = null;
                    while( (line = br.readLine() ) != null ) {
                        String res = parseResponseLine(line);
                        if(!TextUtils.isEmpty(res)) {
                            AccountBalanceHelper.this.sendMessage(AccountBalanceHelper.this.obtainMessage(DID_SUCCEED, res));
                            break;
                        }
                    }
                    
                }else {
                    AccountBalanceHelper.this.sendMessage(AccountBalanceHelper.this.obtainMessage(DID_ERROR));
                }
            } catch (Exception e) {
                AccountBalanceHelper.this.sendMessage(AccountBalanceHelper.this.obtainMessage(DID_ERROR));
            }
        }
    };
    t.start();
}
 
开发者ID:treasure-lau,项目名称:CSipSimple,代码行数:36,代码来源:AccountBalanceHelper.java


注:本文中的org.apache.http.client.HttpClient.execute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。