org.apache.commons.httpclient.DefaultHttpMethodRetryHandler Java Examples

The following examples show how to use org.apache.commons.httpclient.DefaultHttpMethodRetryHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpClientIT.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Test
public void hostConfig() {
    HttpClient client = new HttpClient();
    client.getParams().setConnectionManagerTimeout(CONNECTION_TIMEOUT);
    client.getParams().setSoTimeout(SO_TIMEOUT);

    HostConfiguration config = new HostConfiguration();
    config.setHost("weather.naver.com", 80, "http");
    GetMethod method = new GetMethod("/rgn/cityWetrMain.nhn");

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
    method.setQueryString(new NameValuePair[] { new NameValuePair("key2", "value2") });

    try {
        // Execute the method.
        client.executeMethod(config, method);
    } catch (Exception ignored) {
    } finally {
        method.releaseConnection();
    }

    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
}
 
Example #2
Source File: HttpClientIT.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
    HttpClient client = new HttpClient();
    client.getParams().setConnectionManagerTimeout(CONNECTION_TIMEOUT);
    client.getParams().setSoTimeout(SO_TIMEOUT);

    GetMethod method = new GetMethod(webServer.getCallHttpUrl());

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
    method.setQueryString(new NameValuePair[] { new NameValuePair("key2", "value2") });

    try {
        // Execute the method.
        client.executeMethod(method);
    } catch (Exception ignored) {
    } finally {
        method.releaseConnection();
    }

    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
}
 
Example #3
Source File: GobblinHttpMethodRetryHandlerTest.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryMethod() {
  GobblinHttpMethodRetryHandler gobblinHttpMethodRetryHandler = new GobblinHttpMethodRetryHandler(1, false);
  HttpMethod mockHttpMethod = Mockito.mock(HttpMethod.class);

  //GobblinHttpHandler.retryMethod should return true on UnknownHostException
  Assert.assertTrue(gobblinHttpMethodRetryHandler.retryMethod(mockHttpMethod, new UnknownHostException("dummyException"), 0));
  Assert.assertTrue(gobblinHttpMethodRetryHandler.retryMethod(mockHttpMethod, new UnknownHostException("dummyException"), 1));
  //Return false when the retry count is exceeded
  Assert.assertFalse(gobblinHttpMethodRetryHandler.retryMethod(mockHttpMethod, new UnknownHostException("dummyException"), 2));

  //Ensure the GobblinHttpMethodRetryHandler has the same behavior as the DefaultHttpMethodRetryHandler for a normal
  //IOException
  DefaultHttpMethodRetryHandler defaultHttpMethodRetryHandler = new DefaultHttpMethodRetryHandler(1, false);
  boolean shouldRetryWithGobblinRetryHandler = gobblinHttpMethodRetryHandler.retryMethod(mockHttpMethod, new IOException("dummyException"), 0);
  boolean shouldRetryWithDefaultRetryHandler = defaultHttpMethodRetryHandler.retryMethod(mockHttpMethod, new IOException("dummyException"), 0);
  Assert.assertTrue(shouldRetryWithGobblinRetryHandler);
  Assert.assertEquals(shouldRetryWithDefaultRetryHandler, shouldRetryWithGobblinRetryHandler);

  shouldRetryWithGobblinRetryHandler = gobblinHttpMethodRetryHandler.retryMethod(mockHttpMethod, new IOException("dummyException"), 2);
  shouldRetryWithDefaultRetryHandler = defaultHttpMethodRetryHandler.retryMethod(mockHttpMethod, new IOException("dummyException"), 2);
  Assert.assertFalse(shouldRetryWithGobblinRetryHandler);
  Assert.assertEquals(shouldRetryWithDefaultRetryHandler, shouldRetryWithGobblinRetryHandler);
}
 
Example #4
Source File: ChuangRuiSMSProvider.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
public MessageResult sendMessage(String mobile, String content,SmsDTO smsDTO) throws Exception{
    log.info("sms content={}", content);

    HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod("http://api.1cloudsp.com/api/v2/single_send");
    postMethod.getParams().setContentCharset("UTF-8");
    postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());


    NameValuePair[] data = {
            new NameValuePair("accesskey", smsDTO.getKeyId()),
            new NameValuePair("secret", smsDTO.getKeySecret()),
            new NameValuePair("sign", smsDTO.getSignId()),
            new NameValuePair("templateId", smsDTO.getTemplateId()),
            new NameValuePair("mobile", mobile),
            new NameValuePair("content", URLEncoder.encode(content, "utf-8"))//(发送的短信内容是模板变量内容,多个变量中间用##或者$$隔开,采用utf8编码)
    };
    postMethod.setRequestBody(data);

    int statusCode = httpClient.executeMethod(postMethod);
    System.out.println("statusCode: " + statusCode + ", body: "
            + postMethod.getResponseBodyAsString());


    log.info(" mobile : " + mobile + "content : " + content);
    log.info("statusCode: " + statusCode + ", body: "
            + postMethod.getResponseBodyAsString());
    return parseResult(postMethod.getResponseBodyAsString());
}
 
Example #5
Source File: BaseHttpRequestMaker.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static boolean sendInfoToServer(NameValuePair[] pairs, String URI, StringBuffer response, int retryCount) {
	HttpClient client = new HttpClient();
	GetMethod method = new GetMethod(URI);
	method.setQueryString(pairs);

//	if (retryCount>0)
		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(retryCount, false));
	
	return executeMethod(client, method, response);
}
 
Example #6
Source File: BaseHttpRequestMaker.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static boolean postToServer(String s, String URI, StringBuffer response) {
	HttpClient client = new HttpClient();
	PostMethod method = new PostMethod(URI);
	method.addParameter("OS", StringEscapeUtils.escapeHtml3(System.getProperty("os.name") + "\t" + System.getProperty("os.version")));
	method.addParameter("JVM", StringEscapeUtils.escapeHtml3(System.getProperty("java.version") +"\t" + System.getProperty("java.vendor")));
	NameValuePair post = new NameValuePair();
	post.setName("post");
	post.setValue(StringEscapeUtils.escapeHtml3(s));
	method.addParameter(post);

	method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
    		new DefaultHttpMethodRetryHandler(3, false));
	
	return executeMethod(client, method, response);
}
 
Example #7
Source File: BaseHttpRequestMaker.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static boolean contactServer(String s, String URI, StringBuffer response) {
	HttpClient client = new HttpClient();
	GetMethod method = new GetMethod(URI);
	NameValuePair[] pairs = new NameValuePair[1];
	pairs[0] = new NameValuePair("build", StringEscapeUtils.escapeHtml3("\t" + s + "\tOS =\t" + System.getProperty("os.name") + "\t" + System.getProperty("os.version") + "\tjava =\t" + System.getProperty("java.version") +"\t" + System.getProperty("java.vendor")));
	method.setQueryString(pairs);

	method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
    		new DefaultHttpMethodRetryHandler(3, false));
	
	return executeMethod(client, method, response);
}
 
Example #8
Source File: Telegram.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
private static PostMethod createPostMethod(String url, int timeOut, int retries) {
    PostMethod postMethod = new PostMethod(url);
    postMethod.getParams().setContentCharset("UTF-8");
    postMethod.getParams().setSoTimeout(timeOut);
    postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(retries, false));
    return postMethod;
}
 
Example #9
Source File: NetClient.java    From qingyang with Apache License 2.0 5 votes vote down vote up
private static HttpClient getHttpClient() {
	HttpClient httpClient = new HttpClient();

	// 设置默认的超时重试处理策略
	httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
			new DefaultHttpMethodRetryHandler());
	// 设置连接超时时间
	httpClient.getHttpConnectionManager().getParams()
			.setConnectionTimeout(TIMEOUT_CONNECTION);
	httpClient.getParams().setContentCharset(UTF_8);
	return httpClient;
}
 
Example #10
Source File: HttpClientBuilder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Builds an HTTP client with the given settings. Settings are NOT reset to their default values after a client has
 * been created.
 * 
 * @return the created client.
 */
public HttpClient buildClient() {
    if (httpsProtocolSocketFactory != null) {
        Protocol.registerProtocol("https", new Protocol("https", httpsProtocolSocketFactory, 443));
    }

    HttpClientParams clientParams = new HttpClientParams();
    clientParams.setAuthenticationPreemptive(isPreemptiveAuthentication());
    clientParams.setContentCharset(getContentCharSet());
    clientParams.setParameter(HttpClientParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(
            connectionRetryAttempts, false));

    HttpConnectionManagerParams connMgrParams = new HttpConnectionManagerParams();
    connMgrParams.setConnectionTimeout(getConnectionTimeout());
    connMgrParams.setDefaultMaxConnectionsPerHost(getMaxConnectionsPerHost());
    connMgrParams.setMaxTotalConnections(getMaxTotalConnections());
    connMgrParams.setReceiveBufferSize(getReceiveBufferSize());
    connMgrParams.setSendBufferSize(getSendBufferSize());
    connMgrParams.setTcpNoDelay(isTcpNoDelay());

    MultiThreadedHttpConnectionManager connMgr = new MultiThreadedHttpConnectionManager();
    connMgr.setParams(connMgrParams);

    HttpClient httpClient = new HttpClient(clientParams, connMgr);

    if (proxyHost != null) {
        HostConfiguration hostConfig = new HostConfiguration();
        hostConfig.setProxy(proxyHost, proxyPort);
        httpClient.setHostConfiguration(hostConfig);

        if (proxyUsername != null) {
            AuthScope proxyAuthScope = new AuthScope(proxyHost, proxyPort);
            UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(proxyUsername,
                    proxyPassword);
            httpClient.getState().setProxyCredentials(proxyAuthScope, proxyCredentials);
        }
    }

    return httpClient;
}
 
Example #11
Source File: ChuangRuiSMSProvider.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
public MessageResult sendMessage(String mobile, String content,SmsDTO smsDTO) throws Exception{
    log.info("sms content={}", content);

    HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod("http://api.1cloudsp.com/api/v2/single_send");
    postMethod.getParams().setContentCharset("UTF-8");
    postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());


    NameValuePair[] data = {
            new NameValuePair("accesskey", smsDTO.getKeyId()),
            new NameValuePair("secret", smsDTO.getKeySecret()),
            new NameValuePair("sign", smsDTO.getSignId()),
            new NameValuePair("templateId", smsDTO.getTemplateId()),
            new NameValuePair("mobile", mobile),
            new NameValuePair("content", URLEncoder.encode(content, "utf-8"))//(发送的短信内容是模板变量内容,多个变量中间用##或者$$隔开,采用utf8编码)
    };
    postMethod.setRequestBody(data);

    int statusCode = httpClient.executeMethod(postMethod);
    System.out.println("statusCode: " + statusCode + ", body: "
            + postMethod.getResponseBodyAsString());


    log.info(" mobile : " + mobile + "content : " + content);
    log.info("statusCode: " + statusCode + ", body: "
            + postMethod.getResponseBodyAsString());
    return parseResult(postMethod.getResponseBodyAsString());
}
 
Example #12
Source File: UrlEncordedFormPostViaProxyTestCase.java    From product-ei with Apache License 2.0 4 votes vote down vote up
@SetEnvironment(executionEnvironments = {ExecutionEnvironment.STANDALONE})
@Test(groups = {"wso2.esb"}, description = "Patch : ESBJAVA-1696 : Encoded Special characters in the URL is decoded at the Gateway and not re-encoded", enabled = true)
public void testEncodingSpecialCharacterViaHttpProxy() throws IOException {
    HttpClient client = new HttpClient();
    client.getParams().setParameter(
            HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(0, false));
    client.getParams().setSoTimeout(5000);
    client.getParams().setConnectionManagerTimeout(5000);

    // Create POST method
    String url = getProxyServiceURLHttp("MyProxy");
    PostMethod method = new PostMethod(url);
    // Set parameters on POST
    String value1 = "Hello World";
    String value2 = "This is a Form Submission containing %";
    String value3 = URLEncoder.encode("This is an encoded value containing %");
    method.setParameter("test1", value1);
    method.addParameter("test2", value2);
    method.addParameter("test3", value3);


    // Execute and print response
    try {
        client.executeMethod(method);
    } catch (Exception e) {

    } finally {
        method.releaseConnection();
    }
    String response = wireMonitorServer.getCapturedMessage();
    String[] responseArray = response.split("test");

    if (responseArray.length < 3) {
        Assert.fail("All attributes are not sent");
    }
    for (String res : responseArray) {
        if (res.startsWith("1")) {
            Assert.assertTrue(res.startsWith("1=" + URLEncoder.encode(value1).replace("+", "%20")));
        } else if (res.startsWith("2")) {
            Assert.assertTrue(res.startsWith("2=" + URLEncoder.encode(value2).replace("+", "%20")));
        } else if (res.startsWith("3")) {
            Assert.assertTrue(res.startsWith("3=" + URLEncoder.encode(value3)));
        }
    }

}
 
Example #13
Source File: ApacheHttp31SLRFactory.java    From webarchive-commons with Apache License 2.0 4 votes vote down vote up
@Override
  public void setNumRetries(int numRetries) {
http.getParams().setParameter(HttpClientParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(numRetries, true));
  }
 
Example #14
Source File: HttpUtil.java    From openhab1-addons with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Executes the given <code>url</code> with the given <code>httpMethod</code>
 *
 * @param httpMethod the HTTP method to use
 * @param url the url to execute (in milliseconds)
 * @param httpHeaders optional HTTP headers which has to be set on request
 * @param content the content to be send to the given <code>url</code> or
 *            <code>null</code> if no content should be send.
 * @param contentType the content type of the given <code>content</code>
 * @param timeout the socket timeout to wait for data
 * @param proxyHost the hostname of the proxy
 * @param proxyPort the port of the proxy
 * @param proxyUser the username to authenticate with the proxy
 * @param proxyPassword the password to authenticate with the proxy
 * @param nonProxyHosts the hosts that won't be routed through the proxy
 * @return the response body or <code>NULL</code> when the request went wrong
 */
public static String executeUrl(String httpMethod, String url, Properties httpHeaders, InputStream content,
        String contentType, int timeout, String proxyHost, Integer proxyPort, String proxyUser,
        String proxyPassword, String nonProxyHosts) {

    HttpClient client = new HttpClient();

    // only configure a proxy if a host is provided
    if (StringUtils.isNotBlank(proxyHost) && proxyPort != null && shouldUseProxy(url, nonProxyHosts)) {
        client.getHostConfiguration().setProxy(proxyHost, proxyPort);
        if (StringUtils.isNotBlank(proxyUser)) {
            client.getState().setProxyCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(proxyUser, proxyPassword));
        }
    }

    HttpMethod method = HttpUtil.createHttpMethod(httpMethod, url);
    method.getParams().setSoTimeout(timeout);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
    if (httpHeaders != null) {
        for (String httpHeaderKey : httpHeaders.stringPropertyNames()) {
            method.addRequestHeader(new Header(httpHeaderKey, httpHeaders.getProperty(httpHeaderKey)));
        }
    }
    // add content if a valid method is given ...
    if (method instanceof EntityEnclosingMethod && content != null) {
        EntityEnclosingMethod eeMethod = (EntityEnclosingMethod) method;
        eeMethod.setRequestEntity(new InputStreamRequestEntity(content, contentType));
    }

    Credentials credentials = extractCredentials(url);
    if (credentials != null) {
        client.getParams().setAuthenticationPreemptive(true);
        client.getState().setCredentials(AuthScope.ANY, credentials);
    }

    if (logger.isDebugEnabled()) {
        try {
            logger.debug("About to execute '{}'", method.getURI());
        } catch (URIException e) {
            logger.debug("{}", e.getMessage());
        }
    }

    try {

        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            logger.debug("Method failed: {}", method.getStatusLine());
        }

        String responseBody = IOUtils.toString(method.getResponseBodyAsStream());
        if (!responseBody.isEmpty()) {
            logger.debug("{}", responseBody);
        }

        return responseBody;
    } catch (HttpException he) {
        logger.error("Fatal protocol violation: {}", he.toString());
    } catch (IOException ioe) {
        logger.error("Fatal transport error: {}", ioe.toString());
    } finally {
        method.releaseConnection();
    }

    return null;
}
 
Example #15
Source File: FrontierSiliconRadioConnection.java    From openhab1-addons with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Performs a request to the radio with addition parameters.
 * 
 * Typically used for changing parameters.
 * 
 * @param REST
 *            API requestString, e.g. "SET/netRemote.sys.power"
 * @param params
 *            , e.g. "value=1"
 * @return request result
 */
public FrontierSiliconRadioApiResult doRequest(String requestString, String params) {

    // 3 retries upon failure
    for (int i = 0; i < 3; i++) {
        if (!isLoggedIn && !doLogin()) {
            continue; // not logged in and login was not successful - try again!
        }

        final String url = "http://" + hostname + ":" + port + "/fsapi/" + requestString + "?pin=" + pin + "&sid="
                + sessionId + (params == null || params.trim().length() == 0 ? "" : "&" + params);

        logger.trace("calling url: '" + url + "'");

        final HttpMethod method = new GetMethod(url);
        method.getParams().setSoTimeout(SOCKET_TIMEOUT);
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        try {

            final int statusCode = httpClient.executeMethod(method);
            if (statusCode != HttpStatus.SC_OK) {
                logger.warn("Method failed: " + method.getStatusLine());
                isLoggedIn = false;
                method.releaseConnection();
                continue;
            }

            final String responseBody = IOUtils.toString(method.getResponseBodyAsStream());
            if (!responseBody.isEmpty()) {
                logger.trace("got result: " + responseBody);
            } else {
                logger.debug("got empty result");
                isLoggedIn = false;
                method.releaseConnection();
                continue;
            }

            final FrontierSiliconRadioApiResult result = new FrontierSiliconRadioApiResult(responseBody);
            if (result.isStatusOk()) {
                return result;
            }

            isLoggedIn = false;
            method.releaseConnection();
            continue; // try again
        } catch (HttpException he) {
            logger.error("Fatal protocol violation: {}", he.toString());
            isLoggedIn = false;
        } catch (IOException ioe) {
            logger.error("Fatal transport error: {}", ioe.toString());
        } finally {
            method.releaseConnection();
        }
    }
    isLoggedIn = false; // 3 tries failed. log in again next time, maybe our session went invalid (radio restarted?)
    return null;
}
 
Example #16
Source File: UrlEncordedFormPostViaProxyTestCase.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.STANDALONE })
@Test(groups = {
        "wso2.esb" }, description = "Patch : ESBJAVA-1696 : Encoded Special characters in the URL is decoded at the Gateway and not re-encoded", enabled = true)
public void testEncodingSpecialCharacterViaHttpProxy() throws IOException {
    HttpClient client = new HttpClient();
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
    client.getParams().setSoTimeout(5000);
    client.getParams().setConnectionManagerTimeout(5000);

    // Create POST method
    String url = getProxyServiceURLHttp("MyProxy");
    PostMethod method = new PostMethod(url);
    // Set parameters on POST
    String value1 = "Hello World";
    String value2 = "This is a Form Submission containing %";
    String value3 = URLEncoder.encode("This is an encoded value containing %");
    method.setParameter("test1", value1);
    method.addParameter("test2", value2);
    method.addParameter("test3", value3);

    // Execute and print response
    try {
        client.executeMethod(method);
    } catch (Exception e) {

    } finally {
        method.releaseConnection();
    }
    String response = wireMonitorServer.getCapturedMessage();
    String[] responseArray = response.split("test");

    if (responseArray.length < 3) {
        Assert.fail("All attributes are not sent");
    }
    for (String res : responseArray) {
        if (res.startsWith("1")) {
            Assert.assertTrue(res.startsWith("1=" + URLEncoder.encode(value1).replace("+", "%20")));
        } else if (res.startsWith("2")) {
            Assert.assertTrue(res.startsWith("2=" + URLEncoder.encode(value2).replace("+", "%20")));
        } else if (res.startsWith("3")) {
            Assert.assertTrue(res.startsWith("3=" + URLEncoder.encode(value3)));
        }
    }

}
 
Example #17
Source File: GoogleAjaxSearcher.java    From search with Apache License 2.0 4 votes vote down vote up
@Override
public SearchResult search(String keyword, int page) {
    int pageSize = 8;
    //谷歌搜索结果每页大小为8,start参数代表的是返回结果的开始数
    //如获取第一页则start=0,第二页则start=10,第三页则start=20,以此类推,抽象出模式:(page-1)*pageSize
    String url = "http://ajax.googleapis.com/ajax/services/search/web?start="+(page-1)*pageSize+"&rsz=large&v=1.0&q=" + keyword;
    
    SearchResult searchResult = new SearchResult();
    searchResult.setPage(page);
    List<Webpage> webpages = new ArrayList<>();
    try {
        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod(url);

        httpClient.executeMethod(getMethod);
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler());

        int statusCode = httpClient.executeMethod(getMethod);
        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("搜索失败: " + getMethod.getStatusLine());
            return null;
        }
        InputStream in = getMethod.getResponseBodyAsStream();
        byte[] responseBody = Tools.readAll(in);
        String response = new String(responseBody, "UTF-8");
        LOG.debug("搜索返回数据:" + response);
        JSONObject json = new JSONObject(response);
        String totalResult = json.getJSONObject("responseData").getJSONObject("cursor").getString("estimatedResultCount");
        int totalResultCount = Integer.parseInt(totalResult);
        LOG.info("搜索返回记录数: " + totalResultCount);
        searchResult.setTotal(totalResultCount);

        JSONArray results = json.getJSONObject("responseData").getJSONArray("results");

        LOG.debug("搜索结果:");
        for (int i = 0; i < results.length(); i++) {
            Webpage webpage = new Webpage();
            JSONObject result = results.getJSONObject(i);
            //提取标题
            String title = result.getString("titleNoFormatting");
            LOG.debug("标题:" + title);
            webpage.setTitle(title);
            //提取摘要
            String summary = result.get("content").toString();
            summary = summary.replaceAll("<b>", "");
            summary = summary.replaceAll("</b>", "");
            summary = summary.replaceAll("\\.\\.\\.", "");
            LOG.debug("摘要:" + summary);
            webpage.setSummary(summary);
            //从URL中提取正文
            String _url = result.get("url").toString();
            webpage.setUrl(_url);
            String content = Tools.getHTMLContent(_url);
            LOG.debug("正文:" + content);
            webpage.setContent(content);
            webpages.add(webpage);
        }
    } catch (IOException | JSONException | NumberFormatException e) {
        LOG.error("执行搜索失败:", e);
    }
    searchResult.setWebpages(webpages);
    return searchResult;
}
 
Example #18
Source File: RetryParameter.java    From Java_NFe with MIT License 3 votes vote down vote up
/**
 * 
 * @param stub
 *            Client connection
 * @param retry
 *            Connection retry
 */
public static void populateRetry(Stub stub, Integer retry) {

    HttpMethodParams methodParams = new HttpMethodParams();

    methodParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(retry, retry != 0));

    stub._getServiceClient().getOptions().setProperty(HTTPConstants.HTTP_METHOD_PARAMS, methodParams);

}
 
Example #19
Source File: ComplienceUtils.java    From scim2-compliance-test-suite with Apache License 2.0 2 votes vote down vote up
/**
 * Shared method for setting config on the HttpMethod. Sets, for example,
 * retry handler.
 * 
 * @param method
 *            Method to add config to.
 */
public static void configureMethod(HttpMethod method) {
    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
}