Example usage for java.net SocketTimeoutException getMessage

List of usage examples for java.net SocketTimeoutException getMessage

Introduction

In this page you can find the example usage for java.net SocketTimeoutException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:zz.pseas.ghost.utils.HttpClinetUtil.java

/**
 * HTTP??//from   ww  w .  ja  v  a2s.  c  o m
 * 
 * @author GS
 * @param url
 * @param para
 * @param cookie
 * @return
 * @throws IOException
 */
public static int getStatusCode(String url, String cookie) throws IOException {
    GetMethod getMethod = new GetMethod(url);
    getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); // ????
    if (!cookie.equals("")) {
        getMethod.setRequestHeader("cookie", cookie);
    }
    int statusCode = 0;
    try {
        statusCode = hc.executeMethod(getMethod);
        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + getMethod.getStatusLine());
        }
    } catch (SocketTimeoutException e) {
        e.printStackTrace();
        LOG.error("??" + e.getMessage());
    } catch (UnknownHostException e) {
        e.printStackTrace();
        LOG.error("??" + e.getMessage());
    } catch (HttpException e) { // ?
        e.printStackTrace();
        LOG.error("?" + e.getMessage());
    } catch (IOException e) { // ?
        e.printStackTrace();
        LOG.error("?" + e.getMessage());
    } finally {
        getMethod.releaseConnection(); // 
    }
    return statusCode;
}

From source file:zz.pseas.ghost.utils.HttpClinetUtil.java

/**
 * @author GS/*from  www.j  av a2 s.  c o m*/
 * @param url
 * @param headers
 * @return
 * @throws IOException
 */
public static String get(String url, Header[] headers) throws IOException {
    String responseBody = null;
    GetMethod getMethod = new GetMethod(url);
    getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); // ????
    for (Header h : headers) {
        getMethod.addRequestHeader(h);
    }
    try {
        int statusCode = hc.executeMethod(getMethod);
        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + getMethod.getStatusLine());
        }
        responseBody = getMethod.getResponseBodyAsString();
    } catch (SocketTimeoutException e) {
        e.printStackTrace();
        LOG.error("??" + e.getMessage());
    } catch (UnknownHostException e) {
        e.printStackTrace();
        LOG.error("??" + e.getMessage());
    } catch (HttpException e) { // ?
        e.printStackTrace();
        LOG.error("?" + e.getMessage());
    } catch (IOException e) { // ?
        e.printStackTrace();
        LOG.error("?" + e.getMessage());
    } finally {
        getMethod.releaseConnection(); // 
    }
    return responseBody;
}

From source file:zz.pseas.ghost.utils.HttpClinetUtil.java

/**
 * @author GS// w  w  w  .ja v a2  s .  com
 * @param url
 * @param para
 *            get??
 * @return
 * @throws IOException
 */
public static String get(String url, Map<String, String> para) throws IOException {
    String responseBody = null;
    GetMethod getMethod = new GetMethod(url);
    getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); // ????
    NameValuePair[] data = new NameValuePair[para.size()];
    int index = 0;
    for (String s : para.keySet()) {
        data[index++] = new NameValuePair(s, para.get(s)); // ??
    }
    getMethod.setQueryString(data); // ?
    try {
        int statusCode = hc.executeMethod(getMethod);
        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + getMethod.getStatusLine());
        }
        responseBody = getMethod.getResponseBodyAsString();
    } catch (SocketTimeoutException e) {
        e.printStackTrace();
        LOG.error("??" + e.getMessage());
    } catch (UnknownHostException e) {
        e.printStackTrace();
        LOG.error("?,UnknownHostException,?" + e.getMessage());
    } catch (HttpException e) { // ?
        e.printStackTrace();
        LOG.error("?" + e.getMessage());
    } catch (IOException e) { // ?
        e.printStackTrace();
        LOG.error("?" + e.getMessage());
    } finally {
        getMethod.releaseConnection(); // 
    }
    return responseBody;
}

From source file:zz.pseas.ghost.utils.HttpClinetUtil.java

/**
 * @author GS/*  w  ww  .  j  a  va 2  s. co m*/
 * @param url
 * @param para
 * @param cookie
 * @return
 * @throws IOException
 */
public static String post(String url, Map<String, String> para, String cookie) throws IOException {
    String responseBody = null;
    PostMethod postMethod = new PostMethod(url);
    NameValuePair[] data = new NameValuePair[para.size()];
    int index = 0;
    for (String s : para.keySet()) {
        data[index++] = new NameValuePair(s, para.get(s));
    }
    postMethod.setRequestBody(data); // ?
    if (!cookie.equals("")) {
        postMethod.setRequestHeader("cookie", cookie);
    }
    try {
        int statusCode = hc.executeMethod(postMethod);
        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + postMethod.getStatusLine());
        }
        responseBody = postMethod.getResponseBodyAsString();
    } catch (SocketTimeoutException e) {
        e.printStackTrace();
        LOG.error("??" + e.getMessage());
    } catch (UnknownHostException e) {
        e.printStackTrace();
        LOG.error("??" + e.getMessage());
    } catch (HttpException e) {
        e.printStackTrace();
        LOG.error(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        LOG.error(e.getMessage());
    } finally {
        postMethod.releaseConnection(); // 
    }
    return responseBody;
}

From source file:com.hoccer.tools.HttpHelper.java

private static HttpResponse executeHTTPMethod(HttpRequestBase pMethod, int pConnectionTimeout)
        throws IOException, HttpClientException, HttpServerException {

    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, pConnectionTimeout);
    HttpConnectionParams.setSoTimeout(httpParams, pConnectionTimeout);
    HttpClientParams.setRedirecting(httpParams, true);

    DefaultHttpClient httpclient = new HttpClientWithKeystore(httpParams);

    // Log redirects
    httpclient.setRedirectHandler(new DefaultRedirectHandler() {
        @Override/*  w  w  w . j  a  v  a 2s .  c  o  m*/
        public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException {
            URI uri = super.getLocationURI(response, context);
            return uri;
        }
    });

    HttpResponse response;
    try {
        response = httpclient.execute(pMethod);
    } catch (SocketTimeoutException e) {
        e = new SocketTimeoutException(e.getMessage() + ": " + pMethod.getURI());
        e.fillInStackTrace();
        throw e;
    } catch (SocketException e) {
        e = new SocketException(e.getMessage() + ": " + pMethod.getURI());
        e.fillInStackTrace();
        throw e;
    }
    HttpException.throwIfError(pMethod.getURI().toString(), response);
    return response;
}

From source file:org.wso2.carbon.governance.registry.extensions.utils.APIUtils.java

/**
 * This method will publish api to APIM 2.0.0.
 * @param httpclient//w w w  . j  a va2 s .  c o  m
 * @param httppost
 * @param params
 * @param httpContext
 * @return
 */
public static ResponseAPIM callAPIMToPublishAPI2(HttpClient httpclient, HttpPost httppost,
        List<NameValuePair> params, HttpContext httpContext) throws GovernanceException {
    try {
        httppost.setEntity(new UrlEncodedFormEntity(params, Constants.UTF_8_ENCODE));
        HttpResponse response = httpclient.execute(httppost, httpContext);
        if (response.getStatusLine().getStatusCode() != Constants.CREATED_RESPONSE_CODE) { // 201 is the successful response status code
            throw new RegistryException(
                    "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, Constants.UTF_8_ENCODE);
        return null;

    } catch (java.net.SocketTimeoutException e) {
        throw new GovernanceException("Connection timed out, Please check the network availability", e);
    } catch (UnsupportedEncodingException e) {
        throw new GovernanceException("Unsupported encode exception.", e);
    } catch (IOException e) {
        throw new GovernanceException("IO Exception occurred.", e);
    } catch (Exception e) {
        throw new GovernanceException(e.getMessage(), e);
    }
}

From source file:org.wso2.carbon.governance.registry.extensions.utils.APIUtils.java

/**
 * This method will publish api to APIM.
 * @param httpclient/*w  w  w  .  j a v  a2 s . c  om*/
 * @param httppost
 * @param params
 * @param httpContext
 * @return
 */
public static ResponseAPIM callAPIMToPublishAPI(HttpClient httpclient, HttpPost httppost,
        List<NameValuePair> params, HttpContext httpContext) throws GovernanceException {
    try {
        httppost.setEntity(new UrlEncodedFormEntity(params, Constants.UTF_8_ENCODE));
        HttpResponse response = httpclient.execute(httppost, httpContext);
        if (response.getStatusLine().getStatusCode() != Constants.SUCCESS_RESPONSE_CODE) { // 200 is the successful response status code
            throw new RegistryException(
                    "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, Constants.UTF_8_ENCODE);
        Gson gson = new Gson();
        return gson.fromJson(responseString, ResponseAPIM.class);

    } catch (java.net.SocketTimeoutException e) {
        throw new GovernanceException("Connection timed out, Please check the network availability", e);
    } catch (UnsupportedEncodingException e) {
        throw new GovernanceException("Unsupported encode exception.", e);
    } catch (IOException e) {
        throw new GovernanceException("IO Exception occurred.", e);
    } catch (Exception e) {
        throw new GovernanceException(e.getMessage(), e);
    }
}

From source file:org.wso2.carbon.governance.registry.extensions.executors.apistore.APIDeleteExecutor.java

/**
 * This method will publish api to APIM.
 *
 * @param httpclient  HttpClient/*  w w  w  . j  av a2  s .  c o  m*/
 * @param httppost    HttpPost
 * @param params      List of NameValuePair
 * @param httpContext HttpContext
 * @return ResponseAPIM
 */
public static ResponseAPIM callAPIMToPublishAPI(HttpClient httpclient, HttpPost httppost,
        List<NameValuePair> params, HttpContext httpContext) throws GovernanceException {
    try {
        httppost.setEntity(new UrlEncodedFormEntity(params, Constants.UTF_8_ENCODE));
        HttpResponse response = httpclient.execute(httppost, httpContext);
        if (response.getStatusLine().getStatusCode() != Constants.SUCCESS_RESPONSE_CODE) {
            // 200 is the successful response status code
            throw new RuntimeException(
                    "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, Constants.UTF_8_ENCODE);
        Gson gson = new Gson();
        return gson.fromJson(responseString, ResponseAPIM.class);

    } catch (java.net.SocketTimeoutException e) {
        throw new GovernanceException("Connection timed out, Please check the network availability", e);
    } catch (UnsupportedEncodingException e) {
        throw new GovernanceException("Unsupported encode exception.", e);
    } catch (IOException e) {
        throw new GovernanceException("IO Exception occurred.", e);
    } catch (Exception e) {
        throw new GovernanceException(e.getMessage(), e);
    }
}

From source file:com.crawler.app.run.CrawlSiteController.java

public static int getPageNumberEnd(String url) {
    try {//from w w  w.  j a v  a 2  s.  c om
        Connection.Response response =

                Jsoup.connect(url)
                        //enable for error urls
                        .ignoreHttpErrors(true)
                        //MAXIMUN TIME
                        .timeout(timeOut)
                        //This is to prevent producing garbage by attempting to parse a JPEG binary image
                        .ignoreContentType(true).execute();

        int status = response.statusCode();
        //after done
        if (status == 200) {
            org.jsoup.nodes.Document doc = response.parse();
            if (!pageNumberSelect.isEmpty()) {
                int pageNumber = -1;
                String strPageNumber = doc.select(pageNumberSelect).text();
                if (pageNumberIndexBegin > -1 && pageNumberIndexEnd > -1) {
                    String strSplit = "0";
                    for (int i = pageNumberIndexBegin; i <= pageNumberIndexEnd; i++) {
                        String ch = String.valueOf(strPageNumber.charAt(i));
                        if (tryParseIntByString(ch)) {
                            strSplit += ch;
                        }
                    }
                    pageNumber = Integer.parseInt(strSplit);
                } else {
                    pageNumber = Integer.parseInt(strPageNumber);
                }
                return pageNumber;

            } else {
                // get pagenumber with total product/numberproduct in page
                String strTotalProduct = "";
                if (totalProductPosition > -1) {
                    if (totalProductSelectPosition.isEmpty()) {
                        strTotalProduct = doc.select(totalProductSelect).get(totalProductPosition).text();
                    } else {
                        strTotalProduct = doc.select(totalProductSelect).get(totalProductPosition)
                                .select(totalProductSelectPosition).text();
                    }
                } else {
                    strTotalProduct = doc.select(totalProductSelect).text().toString().trim();
                }
                int totalProduct = -1;
                int i;
                // get totalproduct
                if (totalProductIndexBegin < 0) {
                    String strNumberTotalProduct = "0";
                    for (i = 0; i < strTotalProduct.length(); i++) {
                        String ch = String.valueOf(strTotalProduct.charAt(i));
                        if (ch.isEmpty() || ch.equals(" ")) {
                            break;
                        }
                        if (tryParseIntByString(ch)) {
                            strNumberTotalProduct += ch;
                        }
                    }
                    totalProduct = Integer.parseInt(strNumberTotalProduct);
                } else {
                    String strSplit = "0";
                    for (i = totalProductIndexBegin; i <= totalProductIndexEnd; i++) {
                        String ch = String.valueOf(strTotalProduct.charAt(i));
                        if (tryParseIntByString(ch)) {
                            strSplit += ch;
                        }
                    }
                    totalProduct = Integer.parseInt(strSplit);
                }
                // get number product in page
                int numberPage = -1;
                String strNumberProductInPage = "";
                if (numberProductInPagePosition > -1) {
                    if (numberProductInPageSelectPosition.isEmpty()) {
                        strNumberProductInPage = doc.select(numberProductInPageSelect)
                                .get(numberProductInPagePosition).text();
                    } else {
                        strNumberProductInPage = doc.select(numberProductInPageSelect)
                                .get(numberProductInPagePosition).select(numberProductInPageSelectPosition)
                                .text();
                    }
                } else {
                    strNumberProductInPage = doc.select(numberProductInPageSelect).text();
                }

                if (regexFromIndexEnd < 0 && regexToIndexEnd < 0) {
                    String strSplit = "0";
                    for (i = 0; i < strNumberProductInPage.length(); i++) {
                        String ch = String.valueOf(strNumberProductInPage.charAt(i));
                        if (ch.isEmpty() || ch.equals(" ")) {
                            break;
                        }
                        if (tryParseIntByString(ch)) {
                            strSplit += ch;
                        }
                    }
                    int numberProductInPage = Integer.parseInt(strSplit);
                    if (totalProduct > -1 && numberProductInPage > 0) {
                        if ((totalProduct % numberProductInPage) == 0) {
                            numberPage = totalProduct / numberProductInPage;
                        } else if (totalProduct > numberProductInPage) {
                            numberPage = totalProduct / numberProductInPage + 1;
                        }
                    }
                    return numberPage;
                } else {
                    //String[] arrStrNumberProductInPage = strNumberProductInPage.split(regex);
                    //String strNumberProductFrom = arrStrNumberProductInPage[0];
                    String strTotalProductReplace = String.valueOf(totalProduct);
                    if (!decimalFormatTotalProduct.isEmpty()) {
                        DecimalFormat formatter = new DecimalFormat(decimalFormatTotalProduct);
                        strTotalProductReplace = formatter.format(totalProduct);
                    }
                    strNumberProductInPage = strNumberProductInPage.replace(strTotalProductReplace, "");
                    int numberFrom = -1;
                    String strSplitFrom = "0";
                    for (i = regexFromIndexBegin; i <= regexFromIndexEnd; i++) {
                        String ch = String.valueOf(strNumberProductInPage.charAt(i));
                        if (tryParseIntByString(ch)) {
                            strSplitFrom += ch;
                        }
                    }
                    numberFrom = Integer.parseInt(strSplitFrom);

                    // String strNumberProductTo = arrStrNumberProductInPage[1];
                    int numberTo = -1;
                    String strSplitTo = "0";
                    for (i = regexToIndexBegin; i <= regexToIndexEnd; i++) {
                        String ch = String.valueOf(strNumberProductInPage.charAt(i));
                        if (tryParseIntByString(ch)) {
                            strSplitTo += ch;
                        }
                    }
                    numberTo = Integer.parseInt(strSplitTo);
                    int numberProductInPage = numberTo - numberFrom + 1;
                    if (totalProduct > -1 && numberProductInPage > 0) {
                        if ((totalProduct % numberProductInPage) == 0) {
                            numberPage = totalProduct / numberProductInPage;
                        } else if (totalProduct > numberProductInPage) {
                            numberPage = totalProduct / numberProductInPage + 1;
                        }
                    }
                    return numberPage;
                }
            }

        } else {
            return -1;
        }

    } catch (SocketTimeoutException se) {

        System.out.println("getContentOnly: SocketTimeoutException");
        System.out.println(se.getMessage());
        return -1;
    }

    catch (Exception e) {

        System.out.println("getContentOnly: Exception");
        e.printStackTrace();
        return -1;
    }

}

From source file:io.cloudslang.content.httpclient.execute.HttpClientExecutor.java

public CloseableHttpResponse execute() {
    CloseableHttpResponse response;/* w w  w  .  j  a v  a 2s .  c  om*/
    try {
        response = closeableHttpClient.execute(httpRequestBase, context);
    } catch (SocketTimeoutException ste) {
        throw new RuntimeException("Socket timeout: " + ste.getMessage(), ste);
    } catch (HttpHostConnectException connectEx) {
        throw new RuntimeException("Connection error: " + connectEx.getMessage(), connectEx);
    } catch (IOException e) {
        throw new RuntimeException("Error while executing http request: " + e.getMessage(), e);
    }
    return response;
}