本文整理匯總了Java中java.net.URLConnection.getHeaderFieldKey方法的典型用法代碼示例。如果您正苦於以下問題:Java URLConnection.getHeaderFieldKey方法的具體用法?Java URLConnection.getHeaderFieldKey怎麽用?Java URLConnection.getHeaderFieldKey使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.net.URLConnection
的用法示例。
在下文中一共展示了URLConnection.getHeaderFieldKey方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getContentTypeHeader
import java.net.URLConnection; //導入方法依賴的package包/類
/**
* 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;
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;
}
示例2: loadHeaders
import java.net.URLConnection; //導入方法依賴的package包/類
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() );
}
}
示例3: loadHeaders
import java.net.URLConnection; //導入方法依賴的package包/類
private void loadHeaders( URLConnection connection ) {
if (HttpUnitOptions.isLoggingHttpHeaders()) {
System.out.println( "Header:: " + connection.getHeaderField(0) );
}
for (int i = 1; true; i++) {
String key = connection.getHeaderFieldKey( i );
if (key == null) break;
if (HttpUnitOptions.isLoggingHttpHeaders()) {
System.out.println( "Header:: " + connection.getHeaderFieldKey( i ) + ": " + connection.getHeaderField(i) );
}
addHeader( connection.getHeaderFieldKey( i ).toUpperCase(), connection.getHeaderField( i ) );
}
if (connection.getContentType() != null) {
setContentTypeHeader( connection.getContentType() );
}
}
示例4: getCookieHttpClient
import java.net.URLConnection; //導入方法依賴的package包/類
/**
* Get a cookie from Google and place it in a ClosableHttpClient
* @return An instance of CloseableHttpClient that is critical to this Class
* @throws IOException When URL connection is improperly formed
*/
private static CloseableHttpClient getCookieHttpClient() throws IOException {
/*
* Tutorial: http://www.hccp.org/java-net-cookie-how-to.html
*/
URL myUrl = new URL("https://trends.google.com");
URLConnection urlConn = myUrl.openConnection();
urlConn.connect();
String headerName = null;
for(int i=1; (headerName = urlConn.getHeaderFieldKey(i)) != null; i++)
if(headerName.equals("Set-Cookie"))
cookieString = urlConn.getHeaderField(i);
cookieString = cookieString.substring(0, cookieString.indexOf(";"));
/*
* Tutorial: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html#d5e499
*/
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("Cookie", cookieString);
cookie.setDomain(".google.com");
cookie.setPath("/trends");
cookieStore.addCookie(cookie);
return HttpClients.custom().setDefaultCookieStore(cookieStore).build();
}