Example usage for java.net URL toString

List of usage examples for java.net URL toString

Introduction

In this page you can find the example usage for java.net URL toString.

Prototype

public String toString() 

Source Link

Document

Constructs a string representation of this URL .

Usage

From source file:com.spoiledmilk.ibikecph.util.HttpUtils.java

public static JsonNode getFromServer(String urlString) {
    JsonNode ret = null;/*  w  w w  .j  av a2  s.  c  o m*/
    LOG.d("GET api request, url = " + urlString);
    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT);
    HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT);
    HttpClient httpclient = new DefaultHttpClient(myParams);
    httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT);
    HttpGet httpget = null;
    URL url = null;
    try {

        url = new URL(urlString);
        httpget = new HttpGet(url.toString());
        httpget.setHeader("Content-type", "application/json");
        httpget.setHeader("Accept", ACCEPT);
        httpget.setHeader("LANGUAGE_CODE", IbikeApplication.getLanguageString());
        HttpResponse response = httpclient.execute(httpget);
        String serverResponse = EntityUtils.toString(response.getEntity());
        LOG.d("API response = " + serverResponse);
        ret = Util.stringToJsonNode(serverResponse);
    } catch (Exception e) {
        if (e != null && e.getLocalizedMessage() != null) {
            LOG.e(e.getLocalizedMessage());
        }
    }
    return ret;
}

From source file:com.spoiledmilk.ibikecph.util.HttpUtils.java

public static JsonNode putToServer(String urlString, JSONObject objectToPost) {
    JsonNode ret = null;//from w  ww .  j  a  v a2s .c o  m
    LOG.d("PUT api request, url = " + urlString);
    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT);
    HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT);
    HttpClient httpclient = new DefaultHttpClient(myParams);
    httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT);
    HttpPut httput = null;
    URL url = null;
    try {
        url = new URL(urlString);
        httput = new HttpPut(url.toString());
        httput.setHeader("Content-type", "application/json");
        httput.setHeader("Accept", ACCEPT);
        httput.setHeader("LANGUAGE_CODE", IbikeApplication.getLanguageString());
        StringEntity se = new StringEntity(objectToPost.toString(), HTTP.UTF_8);
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httput.setEntity(se);
        HttpResponse response = httpclient.execute(httput);
        String serverResponse = EntityUtils.toString(response.getEntity());
        LOG.d("API response = " + serverResponse);
        ret = Util.stringToJsonNode(serverResponse);
    } catch (Exception e) {
        if (e != null && e.getLocalizedMessage() != null) {
            LOG.e(e.getLocalizedMessage());
        }
    }
    return ret;
}

From source file:com.spoiledmilk.ibikecph.search.HTTPAutocompleteHandler.java

public static JsonNode performGET(String urlString) {
    JsonNode ret = null;//  ww  w .ja  v  a 2 s .  c o  m

    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, 20000);
    HttpConnectionParams.setSoTimeout(myParams, 20000);
    HttpClient httpclient = new DefaultHttpClient(myParams);
    HttpGet httpget = null;

    URL url = null;

    try {

        url = new URL(urlString);
        httpget = new HttpGet(url.toString());
        LOG.d("Request " + url.toString());
        httpget.setHeader("Content-type", "application/json");
        HttpResponse response = httpclient.execute(httpget);
        String serverResponse = EntityUtils.toString(response.getEntity());
        LOG.d("Response " + serverResponse);
        ret = Util.stringToJsonNode(serverResponse);

    } catch (Exception e) {
        if (e != null && e.getLocalizedMessage() != null)
            LOG.e(e.getLocalizedMessage());
    }
    return ret;
}

From source file:com.spoiledmilk.ibikecph.util.HttpUtils.java

public static JsonNode postToServer(String urlString, JSONObject objectToPost) {
    JsonNode ret = null;//from www  . ja va 2 s .c  o m
    LOG.d("POST api request, url = " + urlString + " object = " + objectToPost.toString());
    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT);
    HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT);
    HttpClient httpclient = new DefaultHttpClient(myParams);
    httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT);
    HttpPost httppost = null;
    URL url = null;
    try {
        url = new URL(urlString);
        httppost = new HttpPost(url.toString());
        httppost.setHeader("Content-type", "application/json");
        httppost.setHeader("Accept", ACCEPT);
        httppost.setHeader("LANGUAGE_CODE", IbikeApplication.getLanguageString());
        StringEntity se = new StringEntity(objectToPost.toString(), HTTP.UTF_8);// , HTTP.UTF_8
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httppost.setEntity(se);
        HttpResponse response = httpclient.execute(httppost);
        String serverResponse = EntityUtils.toString(response.getEntity());
        LOG.d("API response = " + serverResponse);
        ret = Util.stringToJsonNode(serverResponse);
    } catch (Exception e) {
        if (e != null && e.getLocalizedMessage() != null) {
            LOG.e(e.getLocalizedMessage());
        }
    }
    return ret;
}

From source file:Main.java

/** Returns the document located at the specific URL.
 * @throw IllegalArgumentException if an error occurs. */
static public Document getDocument(URL url) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;//  www.ja v a2 s  .  c  o  m
    try {
        db = dbf.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new IllegalArgumentException(ex);
    }
    Document doc = null;
    try {
        doc = db.parse(url.toString());
    } catch (SAXException ex) {
        throw new IllegalArgumentException(ex);
    } catch (IOException ex) {
        throw new IllegalArgumentException(ex);
    }
    return doc;
}

From source file:com.spoiledmilk.ibikecph.util.HttpUtils.java

public static JsonNode deleteFromServer(String urlString, JSONObject objectToPost) {
    JsonNode ret = null;//  ww w  . jav a2  s.com
    LOG.d("DELETE api request, url = " + urlString);
    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT);
    HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT);
    HttpClient httpclient = new DefaultHttpClient(myParams);
    httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT);
    HTTPDeleteWithBody httpdelete = null;
    URL url = null;
    try {
        url = new URL(urlString);
        httpdelete = new HTTPDeleteWithBody(url.toString());
        httpdelete.setHeader("Content-type", "application/json");
        httpdelete.setHeader("Accept", ACCEPT);
        httpdelete.setHeader("LANGUAGE_CODE", IbikeApplication.getLanguageString());
        StringEntity se = new StringEntity(objectToPost.toString(), HTTP.UTF_8);
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httpdelete.setEntity(se);
        HttpResponse response = httpclient.execute(httpdelete);
        String serverResponse = EntityUtils.toString(response.getEntity());
        LOG.d("API response = " + serverResponse);
        ret = Util.stringToJsonNode(serverResponse);
    } catch (Exception e) {
        if (e != null && e.getLocalizedMessage() != null)
            LOG.e(e.getLocalizedMessage());
    }
    return ret;
}

From source file:circleplus.app.http.AbstractHttpApi.java

private static HttpURLConnection getHttpURLConnection(URL url, int requestMethod, boolean acceptJson)
        throws IOException {
    if (D)//from www  .  j  av a  2  s. com
        Log.d(TAG, "execute method: " + requestMethod + " url: " + url.toString() + " accept JSON ?= "
                + acceptJson);

    String method;
    boolean isPost;
    switch (requestMethod) {
    case REQUEST_METHOD_GET:
        method = "GET";
        isPost = false;
        break;
    case REQUEST_METHOD_POST:
        method = "POST";
        isPost = true;
        break;
    default:
        method = "GET";
        isPost = false;
        break;
    }

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(TIMEOUT);
    conn.setConnectTimeout(TIMEOUT);
    conn.setRequestProperty("Content-Type", JSON_UTF8_CONTENT_TYPE);
    if (isPost && acceptJson) {
        conn.setRequestProperty("Accept", JSON_CONTENT_TYPE);
    }
    conn.setRequestMethod(method);
    /* setDoOutput(true) equals setRequestMethod("POST") */
    conn.setDoOutput(isPost);
    conn.setChunkedStreamingMode(0);
    // Starts the query
    conn.connect();

    return conn;
}

From source file:edu.du.penrose.systems.util.HttpClientUtils_2.java

/**
 * Check if a web page exists, Redirects are NOT followed.
 * //from ww w. j  ava 2s. co  m
 * @param pageToGet
 * @return
 */
static public WebPage getPage(URL pageToGet) {
    WebPage wp = new WebPage();
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(pageToGet.toString());
    method.setFollowRedirects(false);

    try {
        int statusCode = client.executeMethod(method);

        String contents = method.getResponseBodyAsString();

        wp.setStatus(statusCode);
        wp.setWebPage(contents);
    } catch (Exception e) {
        // TBD
    } finally {
        method.releaseConnection();
    }
    return wp;
}

From source file:com.ettoremastrogiacomo.sktradingjava.starters.Temp.java

public static String getYahooQuotes(String symbol) throws Exception {
    //http://real-chart.finance.yahoo.com/table.csv?s=ENEL.MI&d=0&e=26&f=2017&g=d&a=6&b=9&c=2001&ignore=.csv
    URL url = new URL("https://finance.yahoo.com/quote/" + symbol + "/history?p=" + symbol);

    HttpFetch http = new HttpFetch();
    String res = new String(http.HttpGetUrl(url.toString(), Optional.empty(), Optional.empty()));
    int k0 = res.indexOf("consent-form single-page-form single-page-agree-form");

    if (k0 > 0) {
        java.util.HashMap<String, String> pmap = new java.util.HashMap<>();
        Document dy = Jsoup.parse(res);
        Elements els = dy.select(
                "form[class='consent-form single-page-form single-page-agree-form'] input[type='hidden']");
        els.forEach((x) -> {/*from ww  w  . j a  v  a 2 s .  c o  m*/
            pmap.put(x.attr("name"), x.attr("value"));
        });
        HttpURLConnection huc = http.sendPostRequest("https://guce.oath.com/consent", pmap);
        BufferedReader in = new BufferedReader(new InputStreamReader(huc.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        res = response.toString();
        //cookieList = cookieManager.getCookieStore().getCookies();

    }
    int k1 = res.indexOf("CrumbStore");
    int k2 = res.indexOf("\"", k1 + 22);
    String crumb = res.substring(k1 + 21, k2).replace("\"", "").replace("\\u00", "%");
    LOG.info("crumb=" + crumb);
    String u2 = "https://query1.finance.yahoo.com/v7/finance/download/" + symbol + "?period1=0&period2="
            + System.currentTimeMillis() + "&interval=1d&events=history&crumb=" + crumb;
    res = new String(http.HttpGetUrl(u2, Optional.empty(), Optional.of(http.getCookies())));
    LOG.debug("getting " + u2);
    LOG.debug(res);
    return res;
}

From source file:org.paxml.core.PaxmlResource.java

/**
 * Create from a spring resource.//from w ww  .  j  a v  a 2s  . c o m
 * 
 * @param res
 *            the spring resource
 * @return the paxml resource
 */
public static PaxmlResource createFromResource(Resource res) {
    URL url;
    try {
        url = res.getURL();
    } catch (IOException e) {
        throw new PaxmlRuntimeException("Cannot create paxmlResource from spring resource: " + res, e);
    }
    return createFromPath(url.toString());
}