本文整理汇总了Java中java.net.HttpURLConnection.getHeaderFields方法的典型用法代码示例。如果您正苦于以下问题:Java HttpURLConnection.getHeaderFields方法的具体用法?Java HttpURLConnection.getHeaderFields怎么用?Java HttpURLConnection.getHeaderFields使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.HttpURLConnection
的用法示例。
在下文中一共展示了HttpURLConnection.getHeaderFields方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: postLogin
import java.net.HttpURLConnection; //导入方法依赖的package包/类
/**
* 登陆获取 Cookie
*
* @param username
* @param password
* @return
* @throws Exception
*/
private String postLogin(String username, String password) throws Exception {
String meCookie = null;//(String) memcachedManager.get("PLX_" + username + "_" + password);
if (meCookie != null && !meCookie.equals("")) {
this.Cookie = meCookie;
return meCookie;
}
URL url = new URL(PLX + "/WebRoot/LoginAction.do");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded; text/html; charset=UTF-8");
con.setConnectTimeout(1000);
con.setReadTimeout(3000);
OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
osw.write("activity=login&userId=" + username + "&password=" + password);
osw.flush();
osw.close();
/////////////
//从请求中获取cookie列表
String cookieskey = "Set-Cookie";
Map<String, List<String>> maps = con.getHeaderFields();
List<String> coolist = maps.get(cookieskey);
Iterator<String> it = coolist.iterator();
StringBuffer sbu = new StringBuffer();
while (it.hasNext()) {
sbu.append(it.next() + ";");
}
String cookies = sbu.toString();
cookies = cookies.substring(0, cookies.length() - 1);
this.Cookie = cookies;
//memcachedManager.set("PLX_" + username + "_" + password, cookies, 300);
return cookies;
}
示例2: createResponse
import java.net.HttpURLConnection; //导入方法依赖的package包/类
private Response createResponse(HttpURLConnection connection,
boolean newline) throws IOException {
int code = connection.getResponseCode();
InputStream inputStream = null;
String body = null;
String contentType = connection.getHeaderField("content-type");
String useCharset = getCharsetFromContentType(contentType);
//取reponse取得的chsetat為null時,採用預設的charset
if(useCharset == null){
useCharset = mReadCharset;
}
try {
inputStream = getInputStream(connection);
body = readString(inputStream, useCharset, newline);
} catch (IOException e) {
e.printStackTrace();
inputStream = getErrorStream(connection);
body = readString(inputStream, useCharset, newline);
}
return new Response(code, body, connection.getHeaderFields());
}
示例3: printHttpHeaders
import java.net.HttpURLConnection; //导入方法依赖的package包/类
static void printHttpHeaders(
HttpURLConnection connection
) {
Map<String, List<String>> headers = connection.getHeaderFields();
Set<String> keys = headers.keySet();
Iterator<String> itr = keys.iterator();
while( itr.hasNext() )
{
String key = itr.next();
List<String> values = headers.get( key );
StringBuffer sb = new StringBuffer();
sb.append( key ).append( ":" );
Iterator<String> itrV = values.iterator();
while( itrV.hasNext() ) sb.append( " " ).append( itrV.next() );
System.out.println( sb );
}
}
示例4: testStripContentEncoding
import java.net.HttpURLConnection; //导入方法依赖的package包/类
@SmallTest
@Feature({"Cronet"})
@OnlyRunCronetHttpURLConnection
// Test that Cronet strips content-encoding header.
public void testStripContentEncoding() throws Exception {
URL url = new URL(NativeTestServer.getFileURL("/gzipped.html"));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
assertEquals("foo", connection.getHeaderFieldKey(0));
assertEquals("bar", connection.getHeaderField(0));
assertEquals(null, connection.getHeaderField("content-encoding"));
Map<String, List<String>> responseHeaders = connection.getHeaderFields();
assertEquals(1, responseHeaders.size());
assertEquals(200, connection.getResponseCode());
assertEquals("OK", connection.getResponseMessage());
// Make sure Cronet decodes the gzipped content.
assertEquals("Hello, World!", TestUtil.getResponseAsString(connection));
connection.disconnect();
}
示例5: getCookie
import java.net.HttpURLConnection; //导入方法依赖的package包/类
/**
* getCookie
*
* @param conn
* @return
*/
public static String getCookie(HttpURLConnection conn) {
Map<String, List<String>> resHeaders = conn.getHeaderFields();
StringBuffer sBuffer = new StringBuffer();
for (Map.Entry<String, List<String>> entry : resHeaders.entrySet()) {
String name = entry.getKey();
if (name == null)
continue; // http/1.1 line
List<String> values = entry.getValue();
if (name.equalsIgnoreCase("Set-Cookie")) {
for (String value : values) {
if (value == null) {
continue;
}
String cookie = value.substring(0, value.indexOf(";") + 1);
sBuffer.append(cookie);
}
}
}
if (sBuffer.length() > 0) {
return sBuffer.toString();
}
return sBuffer.toString();
}
示例6: getRequestProperties
import java.net.HttpURLConnection; //导入方法依赖的package包/类
protected void
getRequestProperties(
HttpURLConnection con )
{
try{
setProperty( ResourceDownloader.PR_STRING_CONTENT_TYPE, con.getContentType() );
Map headers = con.getHeaderFields();
Iterator it = headers.entrySet().iterator();
while( it.hasNext()){
Map.Entry entry = (Map.Entry)it.next();
String key = (String)entry.getKey();
Object val = entry.getValue();
if ( key != null ){
setProperty( "URL_" + key, val );
}
}
}catch( Throwable e ){
Debug.printStackTrace(e);
}
}
示例7: readRaw
import java.net.HttpURLConnection; //导入方法依赖的package包/类
/**
* Reads the raw response from a server.
* @param connection the HttpURLConnection to read the raw response from.
* @return The raw response from the server.
* @throws IOException if an IOException occurs.
*/
public static synchronized String readRaw(HttpURLConnection connection) throws IOException
{
connection.connect();
Map<String,List<String>> headMap = connection.getHeaderFields();
String raw = "";
//A status line which includes the status code and reason message
// (e.g., HTTP/1.1 200 OK).
raw += connection.getHeaderField(null)+'\n';
//Response header fields
for(String head : headMap.keySet())
{
if(head==null)
continue;
raw += head + ": ";
List<String> vals = headMap.get(head);
//Response header field values (e.g., Content-Type: text/html).
for(String v : vals)
raw += v;
raw += '\n';
}
//An empty line.
raw += '\n';
//An optional message body.
raw += read(connection);
return raw;
}
示例8: getHeaderFields
import java.net.HttpURLConnection; //导入方法依赖的package包/类
/**
* 通过http的HEAD方式获取url返回的http响应头
*
* @param url 资源URL
* @return 响应头封装在Map中
* @throws IOException IO异常
*/
public static Map<String, List<String>> getHeaderFields(String url) throws IOException {
HttpURLConnection conn = null;
try {
conn = getHttpURLConnection(url);
// Request Method
conn.setRequestMethod("HEAD");
return conn.getHeaderFields();
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
示例9: Response
import java.net.HttpURLConnection; //导入方法依赖的package包/类
public Response(HttpURLConnection connection) throws IOException {
this.code = connection.getResponseCode();
this.contentLength = connection.getContentLength();
this.contentType = connection.getContentType();
this.headers = connection.getHeaderFields();
this.data = ByteStreams.toByteArray(connection.getInputStream());
}
示例10: toResponse
import java.net.HttpURLConnection; //导入方法依赖的package包/类
private Response toResponse(HttpURLConnection conn) throws IOException {
int responseCode = conn.getResponseCode();
InputStream bodyStream;
if (responseCode >= 400 || responseCode == -1) {
bodyStream = conn.getErrorStream();
} else {
bodyStream = conn.getInputStream();
}
interceptResponse(conn);
return new Response(responseCode, bodyStream, conn.getHeaderFields(), conn.getContentLength());
}
示例11: bubbleThumbnail
import java.net.HttpURLConnection; //导入方法依赖的package包/类
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("bubble/{urn}/thumbnail")
public Response bubbleThumbnail(
@Context HttpServletRequest request,
@PathParam("urn") String urn
)
throws IOException,
URISyntaxException
{
APIImpl impl = getAPIImpl( request );
if( impl == null )
{
return Response.status( Response.Status.UNAUTHORIZED ).build();
}
String scope[] = { DataRESTAPI.SCOPE_VIEWABLE_READ };
ResultAuthentication result = impl.authenticate( scope );
if( result.isError() )
{
return formatReturn( result );
}
URL url = new URL( impl.getThumbnailURL( urn ) );
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod( "GET" );
result.setAuthHeader( connection );
int httpStatus = connection.getResponseCode();
if( httpStatus > 299 )
{
return Response.status( httpStatus )
.header("Pragma", "no-cache")
.header("Cache-Control", "no-cache")
.entity( connection.getErrorStream() )
.build();
}
// Copy Autodesk headers describing thumbnail
Map<String, List<String>> headers = connection.getHeaderFields();
Iterator<String> itr = headers.keySet().iterator();
Response.ResponseBuilder builder = Response.status( httpStatus );
while( itr.hasNext() )
{
String key = itr.next();
if( key == null ) continue;
if( !key.startsWith( "x-ads" )) continue;
String value = connection.getHeaderField( key );
builder.header( key, value );
}
return builder.entity( connection.getInputStream() )
.header("content-encoding", connection.getContentEncoding() )
.header("content-type", connection.getContentType() )
.build();
}
示例12: HttpResponse
import java.net.HttpURLConnection; //导入方法依赖的package包/类
public HttpResponse(HttpURLConnection urlConnection, byte[] body) {
try {
this.status = urlConnection.getResponseCode();
this.url = urlConnection.getURL().toString();
} catch (IOException e) {
e.printStackTrace();
}
this.headers = urlConnection.getHeaderFields();
this.body = body;
}
示例13: isResponseCookiePresent
import java.net.HttpURLConnection; //导入方法依赖的package包/类
private boolean isResponseCookiePresent(HttpURLConnection proxyConn,
String expectedName, String expectedValue) {
Map<String, List<String>> headerFields = proxyConn.getHeaderFields();
List<String> cookiesHeader = headerFields.get("Set-Cookie");
if (cookiesHeader != null) {
for (String cookie : cookiesHeader) {
HttpCookie c = HttpCookie.parse(cookie).get(0);
if (c.getName().equals(expectedName)
&& c.getValue().equals(expectedValue)) {
return true;
}
}
}
return false;
}
示例14: extractToken
import java.net.HttpURLConnection; //导入方法依赖的package包/类
/**
* Helper method that extracts an authentication token received from a connection.
* <p>
* This method is used by {@link Authenticator} implementations.
*
* @param conn connection to extract the authentication token from.
* @param token the authentication token.
*
* @throws IOException if an IO error occurred.
* @throws AuthenticationException if an authentication exception occurred.
*/
public static void extractToken(HttpURLConnection conn, Token token) throws IOException, AuthenticationException {
int respCode = conn.getResponseCode();
if (respCode == HttpURLConnection.HTTP_OK
|| respCode == HttpURLConnection.HTTP_CREATED
|| respCode == HttpURLConnection.HTTP_ACCEPTED) {
Map<String, List<String>> headers = conn.getHeaderFields();
List<String> cookies = headers.get("Set-Cookie");
if (cookies != null) {
for (String cookie : cookies) {
if (cookie.startsWith(AUTH_COOKIE_EQ)) {
String value = cookie.substring(AUTH_COOKIE_EQ.length());
int separator = value.indexOf(";");
if (separator > -1) {
value = value.substring(0, separator);
}
if (value.length() > 0) {
token.set(value);
}
}
}
}
} else {
token.set(null);
throw new AuthenticationException("Authentication failed, status: " + conn.getResponseCode() +
", message: " + conn.getResponseMessage());
}
}
示例15: extractOkResponseHeaders
import java.net.HttpURLConnection; //导入方法依赖的package包/类
/**
* Extracts OkHttp headers from the supplied {@link java.net.HttpURLConnection}. Only real headers
* are extracted. See {@link #extractStatusLine(java.net.HttpURLConnection)}.
*/
private static Headers extractOkResponseHeaders(
HttpURLConnection httpUrlConnection, Response.Builder okResponseBuilder) {
Map<String, List<String>> javaResponseHeaders = httpUrlConnection.getHeaderFields();
return extractOkHeaders(javaResponseHeaders, okResponseBuilder);
}