Example usage for java.net HttpURLConnection getHeaderFields

List of usage examples for java.net HttpURLConnection getHeaderFields

Introduction

In this page you can find the example usage for java.net HttpURLConnection getHeaderFields.

Prototype

public Map<String, List<String>> getHeaderFields() 

Source Link

Document

Returns an unmodifiable Map of the header fields.

Usage

From source file:silvertrout.plugins.trace.OperatorFinder.java

static private String getOperator(String areaCode, String number) throws Exception {

    // Pre step//  w  ww.  jav a 2s . co m
    // ================================================================
    HttpURLConnection l = (HttpURLConnection) (new URL("https://pts.siriusit.net/net/PTS/etjansterStart"
            + "/svenska/Om_Webbplatsen/Nummerkapacitet_r" + "/Enskilt+nummer").openConnection());
    l.setDoInput(true);
    l.setDoOutput(false);

    // Fetch cookies needed for later.
    String cookies = "";
    for (Map.Entry<String, List<String>> header : l.getHeaderFields().entrySet()) {
        if (header.getKey() != null && header.getKey().equals("Set-Cookie")) {
            for (String s : header.getValue()) {
                cookies += s.substring(0, s.indexOf(";")) + "; ";
            }
        }
    }
    cookies = cookies.substring(0, cookies.length() - 2);

    // Lookup step
    // ================================================================
    URL url = new URL(URL_SERVER + "/" + URL_PATH);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();

    //con.setFollowRedirects(true);
    con.setDoOutput(true);
    con.setRequestProperty("Cookie", cookies);
    con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");

    //con.setRequestMethod("POST");

    // Post request
    OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
    String outdata = "action=search" + "&ndc=" + URLEncoder.encode(areaCode.substring(1), "UTF-8") + "&number="
            + URLEncoder.encode(number, "UTF-8") + "&search=" + URLEncoder.encode("Sk", "UTF-8") + "\r\n";
    out.write(outdata);
    out.close();

    // Read in response
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

    String result = "", decodedString = "";
    while ((decodedString = in.readLine()) != null) {
        result += decodedString;
    }
    in.close();

    // Find number
    int spos = result.indexOf(TAG_START) + TAG_START.length();
    number = result.substring(spos);
    int epos = number.indexOf(TAG_END);
    number = number.substring(0, epos);

    return StringEscapeUtils.unescapeHtml4(number);
}

From source file:org.javaweb.utils.HttpRequestUtils.java

/**
 * HTTP????HttpResponse/*from  w w  w .  ja va 2s.  c  o  m*/
 *
 * @param httpURLConnection
 * @param response
 * @throws IOException
 */
public static void setResponse(HttpURLConnection httpURLConnection, HttpResponse response) throws IOException {
    response.setStatusCode(httpURLConnection.getResponseCode());
    response.setStatusMessage(httpURLConnection.getResponseMessage());
    response.setContentType(httpURLConnection.getContentType());
    response.setHeader(new CaseInsensitiveMap(httpURLConnection.getHeaderFields()));
    response.setLastModified(httpURLConnection.getLastModified());

    // ?Cookie
    setCookies(response);
}

From source file:net.ftb.util.AppUtils.java

public static void debugConnection(URLConnection c, boolean forceDebug) {
    if (Settings.getSettings().getDebugLauncher() || forceDebug) {
        if (!(c instanceof HttpURLConnection)) {
            Logger.logDebug("Something bad just happened.");
        }/* ww w .java2s.c  o m*/

        // for IP we need to use reflection or pass url here and rely on dns caching
        // ref: https://community.oracle.com/thread/2149226

        HttpURLConnection conn = (HttpURLConnection) c;
        int responseCode;
        try {
            responseCode = conn.getResponseCode();
        } catch (Exception e) {
            responseCode = -1;
            Logger.logDebug("failed", e);
        }

        Logger.logDebug("Request type: " + conn.getRequestMethod());
        Logger.logDebug("URL: " + conn.getURL());
        Logger.logDebug("Response code: " + responseCode);
        Logger.logDebug("Headers:\n" + AppUtils.MapListToString(conn.getHeaderFields()));
        Logger.logDebug("Message body\n" + AppUtils.ConnectionToString(conn));
        if (conn.getURL().toString().contains("ftb.cursecdn.com")) {
            DownloadUtils.CloudFlareInspector("http://ftb.cursecdn.com/", true);
        }
    }
}

From source file:cn.kk.exia.MangaDownloader.java

public static final boolean appendCookies(final StringBuffer cookies, final HttpURLConnection conn)
        throws IOException {
    try {/*from   ww w .  jav  a 2s  .  c o  m*/
        boolean changed = false;
        final List<String> values = conn.getHeaderFields().get("Set-Cookie");
        if (values != null) {
            for (final String v : values) {
                if (v.indexOf("deleted") == -1) {
                    if (cookies.length() > 0) {
                        cookies.append("; ");
                    }
                    cookies.append(v.split(";")[0]);
                    changed = true;
                }
            }
        }
        return changed;
    } catch (final Throwable e) {
        throw new IOException(e);
    }
}

From source file:org.apache.rya.accumulo.mr.merge.util.TimeUtils.java

/**
 * Gets the remote machine's system time by checking the DATE field in the header
 * from a HTTP HEAD method response./*from www  .j a v  a2 s. c om*/
 * @param urlString the URL string of the remote machine's web server to connect to.
 * @return the remote machine's system {@link Date} or {@code null}.
 * @throws IOException
 * @throws ParseException
 */
public static Date getRemoteMachineDate(final String urlString) throws IOException, ParseException {
    Date remoteDate = null;
    HttpURLConnection conn = null;
    try {
        final URL url = new URL(urlString);

        // Set up the initial connection
        conn = (HttpURLConnection) url.openConnection();
        // Use HEAD instead of GET so content isn't returned.
        conn.setRequestMethod(HttpMethods.HEAD);
        conn.setDoOutput(false);
        conn.setReadTimeout(10000);

        conn.connect();

        final Map<String, List<String>> header = conn.getHeaderFields();
        for (final String key : header.keySet()) {
            if (key != null && HttpHeaders.DATE.equals(key)) {
                final List<String> data = header.get(key);
                final String dateString = data.get(0);
                final SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
                remoteDate = sdf.parse(dateString);
                break;
            }
        }
    } finally {
        // Close the connection
        conn.disconnect();
    }

    return remoteDate;
}

From source file:org.kymjs.kjframe.http.httpclient.HttpRequestBuilder.java

/**
 * Open the {@link InputStream} of an Http response. This method supports
 * GZIP and DEFLATE responses./*from  w w w .j a v  a 2 s .c o m*/
 */
private static InputStream getInputStream(HttpURLConnection conn) throws IOException {
    final List<String> contentEncodingValues = conn.getHeaderFields().get("Content-Encoding");
    if (contentEncodingValues != null) {
        for (final String contentEncoding : contentEncodingValues) {
            if (contentEncoding != null) {
                if (contentEncoding.contains("gzip")) {
                    return new GZIPInputStream(conn.getInputStream());
                }
                if (contentEncoding.contains("deflate")) {
                    return new InflaterInputStream(conn.getInputStream(), new Inflater(true));
                }
            }
        }
    }
    return conn.getInputStream();
}

From source file:org.kymjs.kjframe.http.httpclient.HttpRequestBuilder.java

/**
 * Open the error {@link InputStream} of an Http response. This method
 * supports GZIP and DEFLATE responses.//  w ww  . ja v a  2  s  .  c o m
 */
private static InputStream getErrorStream(HttpURLConnection conn) throws IOException {
    final List<String> contentEncodingValues = conn.getHeaderFields().get("Content-Encoding");
    if (contentEncodingValues != null) {
        for (final String contentEncoding : contentEncodingValues) {
            if (contentEncoding != null) {
                if (contentEncoding.contains("gzip")) {
                    return new GZIPInputStream(conn.getErrorStream());
                }
                if (contentEncoding.contains("deflate")) {
                    return new InflaterInputStream(conn.getErrorStream(), new Inflater(true));
                }
            }
        }
    }
    return conn.getErrorStream();
}

From source file:com.talk.demo.util.NetworkUtilities.java

public static String authenticate(String username, String password) {
    String authToken = null;//from  w w w . jav a  2s .  c  om
    String csrfToken2 = null;
    try {
        HttpURLConnection conn = HttpRequest.get(AUTH_URI).getConnection();
        //ToDo: cookie header may be null, should fix it.
        List<String> temp = conn.getHeaderFields().get("Set-Cookie");
        String cookieHeader = null;
        String csrfToken = null;
        if (null != temp && !temp.isEmpty()) {
            cookieHeader = temp.get(0);

            Log.d(TAG, "cookie: " + cookieHeader);

            csrfToken = cookieHeader.split(";")[0];
            Log.d(TAG, "csrf token : " + csrfToken);
        }

        HttpRequest request = HttpRequest.post(AUTH_URI);
        String name = username;
        String passwd = password;
        Map<String, String> data = new HashMap<String, String>();
        data.put("username", name);
        data.put("password", passwd);
        if (csrfToken != null)
            data.put("csrfmiddlewaretoken", csrfToken.substring(10));
        Log.d(TAG, "name: " + username + " passwd: " + password);
        // X-CSRFToken
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "text/html");
        if (csrfToken != null)
            headers.put("Cookie", csrfToken);

        request.headers(headers);
        request.followRedirects(false);
        HttpRequest conn4Session = request.form(data);
        conn4Session.code();
        HttpURLConnection sessionConnection = conn4Session.getConnection();
        try {
            int result = sessionConnection.getResponseCode();
            Log.e(TAG, "get response code : " + result);
            List<String> responseList = sessionConnection.getHeaderFields().get("Set-Cookie");

            if (null != responseList) {
                for (String resItem : responseList) {
                    Log.d(TAG, "cookie session: " + resItem);
                    if (resItem.contains("sessionid")) {
                        authToken = resItem.split(";")[0];
                        Log.d(TAG, "session :" + authToken);
                        NetData.setSessionId(authToken);
                    }

                    if (resItem.contains("csrftoken")) {
                        csrfToken2 = resItem.split(";")[0];
                        Log.d(TAG, "csrf token :" + csrfToken2);
                        NetData.setCsrfToken(csrfToken2);
                    }

                }
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } catch (HttpRequestException exception) {
        Log.d(TAG, "exception : " + exception.toString());
        return null;
    } finally {
        Log.v(TAG, "getAuthtoken completing");
    }

    if ((authToken != null) && (authToken.length() > 0)) {
        Log.v(TAG, "Successful authentication");
        return authToken + ";" + csrfToken2;
    } else {
        Log.e(TAG, "Error authenticating");
        return null;
    }

}

From source file:com.talk.demo.util.NetworkUtilities.java

public static void shareRecord(RawRecord raw, String oring, String target)
        throws JSONException, ParseException, IOException {
    HttpURLConnection conn = HttpRequest.get(AUTH_URI).getConnection();

    /*/*  w  w w  . ja va 2 s .c o  m*/
     * cookieHeader may be null cause NullPointerException
     * ToDo: write the whole code completely
     */
    List<String> temp = conn.getHeaderFields().get("Set-Cookie");
    String cookieHeader = null;
    String csrfToken = null;
    if (null != temp && !temp.isEmpty()) {
        cookieHeader = temp.get(0);

        Log.d(TAG, "cookie: " + cookieHeader);

        csrfToken = cookieHeader.split(";")[0];
        Log.d(TAG, "csrf token : " + csrfToken);
    }

    JSONObject jsonRecord = raw.toJSONObject();
    // Prepare our POST data
    final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(PARAM_USERNAME, oring));
    params.add(new BasicNameValuePair("records", jsonRecord.toString()));
    params.add(new BasicNameValuePair("target", target));
    if (csrfToken != null)
        params.add(new BasicNameValuePair("csrfmiddlewaretoken", csrfToken.substring(10)));
    HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
    final HttpPost post = new HttpPost(SHARE_RECORDS_URI);
    post.addHeader(entity.getContentType());
    if (csrfToken != null)
        post.addHeader("Cookie", csrfToken);
    post.setEntity(entity);
    final HttpResponse resp = getHttpClient().execute(post);
    final String response = EntityUtils.toString(resp.getEntity());
    if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        Log.d(TAG, "finish!!");
    }
}

From source file:com.talk.demo.util.NetworkUtilities.java

/**
 * Connects to the SampleSync test server, authenticates the provided
 * username and password./*from  w  ww .j  av a 2 s.  c om*/
 *
 * @param username The server account username
 * @param password The server account password
 * @return String The authentication token returned by the server (or null)
 */
/*
public static String authenticate(String username, String password) {
        
final HttpResponse resp;
final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(PARAM_USERNAME, username));
params.add(new BasicNameValuePair(PARAM_PASSWORD, password));
final HttpEntity entity;
try {
    entity = new UrlEncodedFormEntity(params);
} catch (final UnsupportedEncodingException e) {
    // this should never happen.
    throw new IllegalStateException(e);
}
Log.i(TAG, "Authenticating to: " + AUTH_URI);
final HttpPost post = new HttpPost(AUTH_URI);
post.addHeader(entity.getContentType());
post.setEntity(entity);
try {
    resp = getHttpClient().execute(post);
    String authToken = null;
    if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        InputStream istream = (resp.getEntity() != null) ? resp.getEntity().getContent()
                : null;
        if (istream != null) {
            BufferedReader ireader = new BufferedReader(new InputStreamReader(istream));
            authToken = ireader.readLine().trim();
        }
    }
    if ((authToken != null) && (authToken.length() > 0)) {
        Log.v(TAG, "Successful authentication");
        return authToken;
    } else {
        Log.e(TAG, "Error authenticating" + resp.getStatusLine());
        return null;
    }
} catch (final IOException e) {
    Log.e(TAG, "IOException when getting authtoken", e);
    return null;
} finally {
    Log.v(TAG, "getAuthtoken completing");
}
}
*/

public static String signup(String username, String email, String password) {
    String authToken = null;
    String csrfToken2 = null;
    try {
        HttpURLConnection conn = HttpRequest.get(SIGNUP_URI).getConnection();
        //ToDo: cookie header may be null, should fix it.
        List<String> cookieHeader = conn.getHeaderFields().get("Set-Cookie");
        for (String resItem : cookieHeader) {
            Log.d(TAG, "cookie session: " + resItem);
            if (resItem.contains("sessionid")) {
                authToken = resItem.split(";")[0];
                Log.d(TAG, "session :" + authToken);
            }

            if (resItem.contains("csrftoken")) {
                csrfToken2 = resItem.split(";")[0];
                Log.d(TAG, "csrf token :" + csrfToken2);
            }

        }
        Log.d(TAG, "cookie: " + cookieHeader);

        String csrfToken = csrfToken2;
        Log.d(TAG, "csrf token : " + csrfToken);

        HttpRequest request = HttpRequest.post(SIGNUP_URI);
        String name = username;
        String mail = email;
        String passwd = password;
        Map<String, String> data = new HashMap<String, String>();
        data.put("username", name);
        data.put("email", mail);
        data.put("password", passwd);
        data.put("password_confirm", passwd);
        data.put("csrfmiddlewaretoken", csrfToken.substring(10));
        Log.d(TAG, "name: " + username + " passwd: " + password);
        // X-CSRFToken
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "text/html");
        headers.put("Cookie", csrfToken);
        Log.d(TAG, "our cookie: " + csrfToken);
        request.headers(headers);
        //request.followRedirects(false);
        HttpRequest conn4Session = request.form(data);
        conn4Session.code();
        HttpURLConnection sessionConnection = conn4Session.getConnection();
        try {
            int result = sessionConnection.getResponseCode();
            Log.e(TAG, "get response code : " + result);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } catch (HttpRequestException exception) {
        Log.d(TAG, "exception : " + exception.toString());
        return null;
    } finally {
        Log.v(TAG, "signup completing");
    }

    return "ok";

}