Search in sources :

Example 1 with IApacheHttpClient

use of info.xiancloud.httpclient.apache_http.IApacheHttpClient in project xian by happyyangyuan.

the class App method main.

public static void main(String[] args) throws Exception {
    for (int i = 0; i < 11; i++) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    IApacheHttpClient httpClient = ApacheHttpClient.newInstance("http://192.168.234.130:8080/httpsweb/login", new HashMap<>());
                    try {
                        System.out.println(EntityUtils.toString(httpClient.getHttpResponse().getEntity(), "UTF-8"));
                    } catch (ConnectTimeoutException e) {
                        LOG.error(e);
                        e.printStackTrace();
                    } catch (SocketTimeoutException e) {
                        LOG.error(e);
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ParseException e) {
                        LOG.error(e);
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        LOG.error(e);
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        httpClient.close();
                    } catch (IOException e) {
                        LOG.error(e);
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        LOG.error(e);
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.err.println(String.format("-avaliable:[%s],leased:[%s],pending:[%s]", ApacheHttpConnManager.getAvailable(), ApacheHttpConnManager.getLeased(), ApacheHttpConnManager.getPending()));
                }
            }
        }).start();
    }
    Thread.sleep(1000000000);
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) ParseException(org.apache.http.ParseException) IOException(java.io.IOException) IApacheHttpClient(info.xiancloud.httpclient.apache_http.IApacheHttpClient) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 2 with IApacheHttpClient

use of info.xiancloud.httpclient.apache_http.IApacheHttpClient in project xian by happyyangyuan.

the class BasicAuthApacheHttpClientGetUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String url = msg.get("url", String.class);
    String userName = msg.get("userName", String.class);
    String password = msg.get("password", String.class);
    Map<String, String> headers = msg.get("headers");
    try (IApacheHttpClient httpClient = BasicAuthApacheHttpClient.newInstance(url, userName, password, headers)) {
        JSONObject responseJSON = new JSONObject();
        HttpResponse httpResponse;
        try {
            httpResponse = httpClient.getHttpResponse();
        } catch (ConnectTimeoutException e) {
            return UnitResponse.error(ISocketGroup.CODE_CONNECT_TIMEOUT, null, "Connect timeout: " + url);
        } catch (SocketTimeoutException e) {
            return UnitResponse.error(ISocketGroup.CODE_SOCKET_TIMEOUT, null, "Read timeout: " + url);
        } catch (Throwable e) {
            return UnitResponse.exception(e);
        }
        responseJSON.put("statusLine", new JSONObject() {

            {
                put("statusCode", httpResponse.getStatusLine().getStatusCode());
                put("protocolVersion", httpResponse.getStatusLine().getProtocolVersion());
                put("reasonPhrase", httpResponse.getStatusLine().getReasonPhrase());
            }
        });
        responseJSON.put("allHeaders", httpResponse.getAllHeaders());
        try {
            responseJSON.put("entity", EntityUtils.toString(httpResponse.getEntity(), "UTF-8"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return UnitResponse.success(responseJSON);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) JSONObject(com.alibaba.fastjson.JSONObject) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) IApacheHttpClient(info.xiancloud.httpclient.apache_http.IApacheHttpClient) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 3 with IApacheHttpClient

use of info.xiancloud.httpclient.apache_http.IApacheHttpClient in project xian by happyyangyuan.

the class BasicAuthApacheHttpClientPostUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String url = msg.get("url", String.class);
    String userName = msg.get("userName", String.class);
    String password = msg.get("password", String.class);
    Map<String, String> headers = msg.get("headers");
    String body = msg.get("body", String.class);
    try (IApacheHttpClient httpClient = BasicAuthApacheHttpClient.newInstance(url, userName, password, headers)) {
        JSONObject responseJSON = new JSONObject();
        String resPayload;
        try {
            resPayload = httpClient.post(body);
        } catch (ConnectTimeoutException e) {
            return UnitResponse.error(ISocketGroup.CODE_CONNECT_TIMEOUT, null, "Connect timeout: " + url);
        } catch (SocketTimeoutException e) {
            return UnitResponse.error(ISocketGroup.CODE_SOCKET_TIMEOUT, null, "Read timeout: " + url);
        } catch (Throwable e) {
            return UnitResponse.exception(e);
        }
        responseJSON.put("statusLine", new JSONObject() {

            {
                put("statusCode", "todo");
                put("protocolVersion", "todo");
                put("reasonPhrase", "todo");
            }
        });
        responseJSON.put("allHeaders", "todo");
        responseJSON.put("entity", resPayload);
        return UnitResponse.success(responseJSON);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) JSONObject(com.alibaba.fastjson.JSONObject) IOException(java.io.IOException) IApacheHttpClient(info.xiancloud.httpclient.apache_http.IApacheHttpClient) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 4 with IApacheHttpClient

use of info.xiancloud.httpclient.apache_http.IApacheHttpClient in project xian by happyyangyuan.

the class ApacheHttpClientGetUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String url = msg.get("url", String.class);
    Map<String, String> headers = msg.get("headers");
    Integer readTimeoutInMillis = msg.get("readTimeoutInMillis", Integer.class);
    try (IApacheHttpClient httpClient = ApacheHttpClient.newInstance(url, headers, readTimeoutInMillis)) {
        JSONObject responseJSON = new JSONObject();
        HttpResponse httpResponse;
        try {
            httpResponse = RetryUtil.retryUntilNoException(httpClient::getHttpResponse, XianConfig.getIntValue("apache.httpclient.max.try", 3), ConnectTimeoutException.class);
        } catch (ConnectTimeoutException e) {
            return UnitResponse.error(ISocketGroup.CODE_CONNECT_TIMEOUT, e, "Connect timeout: " + url);
        } catch (SocketTimeoutException e) {
            return UnitResponse.error(ISocketGroup.CODE_SOCKET_TIMEOUT, e, "Read timeout: " + url);
        } catch (Throwable e) {
            return UnitResponse.exception(e);
        }
        responseJSON.put("statusLine", new JSONObject() {

            {
                put("statusCode", httpResponse.getStatusLine().getStatusCode());
                put("protocolVersion", httpResponse.getStatusLine().getProtocolVersion());
                put("reasonPhrase", httpResponse.getStatusLine().getReasonPhrase());
            }
        });
        responseJSON.put("allHeaders", httpResponse.getAllHeaders());
        try {
            responseJSON.put("entity", EntityUtils.toString(httpResponse.getEntity(), "UTF-8"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return UnitResponse.success(responseJSON);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) JSONObject(com.alibaba.fastjson.JSONObject) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) IApacheHttpClient(info.xiancloud.httpclient.apache_http.IApacheHttpClient) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 5 with IApacheHttpClient

use of info.xiancloud.httpclient.apache_http.IApacheHttpClient in project xian by happyyangyuan.

the class ApacheHttpClientPostUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String url = msg.get("url", String.class);
    Map<String, String> headers = msg.get("headers");
    Integer readTimeoutInMillis = msg.get("readTimeoutInMillis", Integer.class);
    try (IApacheHttpClient httpClient = ApacheHttpClient.newInstance(url, headers, readTimeoutInMillis)) {
        JSONObject responseJSON = new JSONObject();
        String responsePayload;
        try {
            responsePayload = RetryUtil.retryUntilNoException(() -> httpClient.post(msg.get("body", String.class)), XianConfig.getIntValue("apache.httpclient.max.try", 3), // 这里对连接超时做重试,总共只尝试三次
            ConnectTimeoutException.class);
        } catch (ConnectTimeoutException e) {
            return UnitResponse.error(ISocketGroup.CODE_CONNECT_TIMEOUT, e, "Connect timeout: " + url);
        } catch (SocketTimeoutException e) {
            return UnitResponse.error(ISocketGroup.CODE_SOCKET_TIMEOUT, e, "Read timeout: " + url);
        } catch (Throwable e) {
            return UnitResponse.exception(e);
        }
        responseJSON.put("statusLine", new JSONObject() {

            {
                put("statusCode", "todo");
                put("protocolVersion", "todo");
                put("reasonPhrase", "todo");
            }
        });
        responseJSON.put("allHeaders", "todo");
        responseJSON.put("entity", responsePayload);
        return UnitResponse.success(responseJSON);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) JSONObject(com.alibaba.fastjson.JSONObject) IOException(java.io.IOException) IApacheHttpClient(info.xiancloud.httpclient.apache_http.IApacheHttpClient) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Aggregations

IApacheHttpClient (info.xiancloud.httpclient.apache_http.IApacheHttpClient)5 IOException (java.io.IOException)5 SocketTimeoutException (java.net.SocketTimeoutException)5 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)5 JSONObject (com.alibaba.fastjson.JSONObject)4 HttpResponse (org.apache.http.HttpResponse)2 ParseException (org.apache.http.ParseException)1