Java Code Examples for java.net.URLConnection#getHeaderFieldKey()

The following examples show how to use java.net.URLConnection#getHeaderFieldKey() . 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: TitleExtractor.java    From jivejdon with Apache License 2.0 6 votes vote down vote up
/**
 * Loops through response headers until Content-Type is found.
 * 
 * @param conn
 * @return ContentType object representing the value of the Content-Type
 *         header
 */
private static ContentType getContentTypeHeader(URLConnection conn) {
	int i = 0;
	boolean moreHeaders = true;
	do {
		String headerName = conn.getHeaderFieldKey(i);
		String headerValue = conn.getHeaderField(i);
		if (headerName != null && headerName.equals("Content-Type"))
			return new ContentType(headerValue);

		i++;
		moreHeaders = headerName != null || headerValue != null;
	} while (moreHeaders);

	return null;
}
 
Example 2
Source File: HttpWebResponse.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void loadHeaders( URLConnection connection ) {
    if (HttpUnitOptions.isLoggingHttpHeaders()) {
        System.out.println( "Header:: " + connection.getHeaderField(0) );
    }
    for (int i = 1; true; i++) {
        String headerFieldKey = connection.getHeaderFieldKey( i );
        String headerField = connection.getHeaderField(i);
        if (headerFieldKey == null || headerField == null) break;
        if (HttpUnitOptions.isLoggingHttpHeaders()) {
            System.out.println( "Header:: " + headerFieldKey + ": " + headerField );
        }
        addHeader( headerFieldKey.toUpperCase(), headerField );
    }

    if (connection.getContentType() != null) {
        setContentTypeHeader( connection.getContentType() );
    }
}
 
Example 3
Source File: HttpUtil.java    From Android-Carbon-Forum with Apache License 2.0 6 votes vote down vote up
public static Boolean saveCookie(Context context, URLConnection connection){
    //获取Cookie
    String headerName=null;
    for (int i=1; (headerName = connection.getHeaderFieldKey(i))!=null; i++) {
        if (headerName.equals("Set-Cookie")) {
            String cookie = connection.getHeaderField(i);
            //将Cookie保存起来
            SharedPreferences mySharedPreferences = context.getSharedPreferences("Session",
                    Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = mySharedPreferences.edit();
            editor.putString("Cookie", cookie);
            editor.apply();
            return true;
        }
    }
    return false;
}
 
Example 4
Source File: HTTPParser.java    From teamengine with Apache License 2.0 6 votes vote down vote up
private static void append_headers(URLConnection uc, Element e) {
    Document doc = e.getOwnerDocument();
    Element headers = doc.createElement("headers");
    e.appendChild(headers);

    for (int i = 0;; i++) {
        String headerKey = uc.getHeaderFieldKey(i);
        String headerValue = uc.getHeaderField(i);
        if (headerKey == null) {
            if (headerValue == null)
                break;
        } else {
            Element header = doc.createElement("header");
            headers.appendChild(header);
            header.setAttribute("name", headerKey);
            header.appendChild(doc.createTextNode(headerValue));
        }
    }
    if (LOGR.isLoggable(Level.FINER)) {
        LOGR.finer(DomUtils.serializeNode(e));
    }
}
 
Example 5
Source File: AjaxHttpRequest.java    From lizzie with GNU General Public License v3.0 5 votes vote down vote up
public static String getConnectionResponseHeaders(URLConnection c) {
  int idx = 0;
  String value;
  StringBuffer buf = new StringBuffer();
  while ((value = c.getHeaderField(idx)) != null) {
    String key = c.getHeaderFieldKey(idx);
    buf.append(key);
    buf.append(": ");
    buf.append(value);
    idx++;
  }
  return buf.toString();
}
 
Example 6
Source File: MediawikiHandler.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
private boolean grabCookies(URLConnection u) {
    String headerName;
    for (int i = 1; (headerName = u.getHeaderFieldKey(i)) != null; i++) {
        if (headerName.equals("Set-Cookie")) {
            String cookie = u.getHeaderField(i);
            cookie = cookie.substring(0, cookie.indexOf(';'));
            String name = cookie.substring(0, cookie.indexOf('='));
            String value = cookie.substring(cookie.indexOf('=') + 1, cookie.length());
            cookies.put(name, value);
        }
    }
    
    return true;
}