本文整理汇总了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();
}