java.net.URLConnection#getHeaderFields ( )源码实例Demo

下面列出了java.net.URLConnection#getHeaderFields ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Tbed   文件: Sentence.java
/**
 * 根据URL请求json数据
 *
 * @return //parm:请求的url链接  返回的是json字符串
 */

public static String getURLContent() {
        String result = "";
    String urlName = "http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1";
        try {
            URL realURL = new URL(urlName);
            URLConnection conn = realURL.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
            conn.connect();
            Map<String, List<String>> map = conn.getHeaderFields();
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += "\n" + line;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
 
源代码2 项目: danyuan-application   文件: AutoGetHtml.java
@SuppressWarnings({ "unused", "rawtypes", "unchecked" })
public static void main(String[] args) throws IOException {
	String web1 = "http://www.oschina.net/";
	String web2 = "http://news.baidu.com/";
	String web3 = "http://linux.cn/";
	String web4 = "http://www.taobao.com/";
	URL url = new URL(web1);
	URLConnection conn = url.openConnection();
	
	Map headers = conn.getHeaderFields();
	Set<String> keys = headers.keySet();
	for (String key : keys) {
		String val = conn.getHeaderField(key);
		System.out.println(key + "    " + val);
	}
	System.out.println(conn.getLastModified());
}
 
源代码3 项目: scava   文件: GitHubImporter.java
private int getNumberOfPage(URLConnection urlConn) {
	int result = 1;
	Map<String, List<String>> map = urlConn.getHeaderFields();
	List<String> numberOfElements = map.get("Link");
	if (numberOfElements != null)
		for (String string : numberOfElements) {
			int indexOfLast = string.lastIndexOf("&page=") + 6;
			string = string.substring(indexOfLast);
			string = string.substring(0, string.indexOf(">"));
			try {
				result = Integer.parseInt(string);
			} catch (NumberFormatException e) {
				result = 1;
			}
		}
	return result;
}
 
源代码4 项目: zap-extensions   文件: WebPage.java
/**
 * Requests the target for HTTP headers
 *
 * @throws IOException
 */
private void getHTTPHeaders() throws IOException {
    URLConnection conn = null;
    while (conn == null) {
        try {
            conn = this.url.openConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // get all headers
    headers = conn.getHeaderFields();
    /*for (Map.Entry<String, List<String>> entry : map.entrySet()) {
    	System.out.println("Key : " + entry.getKey() +
                    " ,Value : " + entry.getValue());
    }*/

    /*//get header by 'key'
    String server = conn.getHeaderField("Server");*/

}
 
源代码5 项目: zap-extensions   文件: WebPage.java
/**
 * Requests the target for HTTP headers
 *
 * @throws IOException
 */
private void getHTTPHeaders() throws IOException {
    URLConnection conn = null;
    while (conn == null) {
        try {
            conn = this.url.openConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // get all headers
    headers = conn.getHeaderFields();
    /*for (Map.Entry<String, List<String>> entry : map.entrySet()) {
    	System.out.println("Key : " + entry.getKey() +
                    " ,Value : " + entry.getValue());
    }*/

    /*//get header by 'key'
    String server = conn.getHeaderField("Server");*/

}
 
源代码6 项目: ignite   文件: WebSessionSelfTest.java
/**
 * Retrieves HttpSession sessionId from Cookie
 *
 * @param conn URLConnection
 * @return sesId
 */
private String getSessionIdFromCookie(URLConnection conn) {
    String sessionCookieValue = null;
    String sesId = null;
    Map<String, List<String>> headerFields = conn.getHeaderFields();
    Set<String> headerFieldsSet = headerFields.keySet();
    Iterator<String> hearerFieldsIter = headerFieldsSet.iterator();

    while (hearerFieldsIter.hasNext()) {
        String headerFieldKey = hearerFieldsIter.next();

        if ("Set-Cookie".equalsIgnoreCase(headerFieldKey)) {
            List<String> headerFieldValue = headerFields.get(headerFieldKey);

            for (String headerValue : headerFieldValue) {
                String[] fields = headerValue.split(";");
                sessionCookieValue = fields[0];
                sesId = sessionCookieValue.substring(sessionCookieValue.indexOf("=") + 1,
                        sessionCookieValue.length());
            }
        }
    }

    return sesId;
}
 
源代码7 项目: APICloud-Studio   文件: APICloudSplashHandler.java
private String getCookie(URLConnection conn) {
	String cookie = null;
	Map<String, List<String>> map = conn.getHeaderFields();
	Set<String> set = map.keySet();
	for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) {
		String key = iterator.next();
		if (key != null && key.equals("Set-Cookie")) {
			List<String> list = map.get(key);
			StringBuilder builder = new StringBuilder();
			for (String str : list) {
				str = str.replaceAll("HttpOnly", ""); //$NON-NLS-2$
				builder.append(str).toString();
				if (str.startsWith("username=")) {
					int endIndex = str.indexOf(";");
					userNameFromCookie = str.substring(9, endIndex);
					userNameFromCookie = userNameFromCookie.replaceAll("%40", "@");
				}
			}
			cookie = builder.toString();
		}
	}
	return cookie;
}
 
源代码8 项目: danyuan-application   文件: AutoGetHtml.java
/**
 * 获取响应头
 * 打印到控制台
 */
public void getHeaders() {
	if (websites == null) {
		System.err.println("查询网址不能为空!");
		return;
	}
	try {
		for (String website : websites) {
			System.out.println("----------------网站:" + website + "HTTP响应头---------------");
			URL url = new URL(website);
			URLConnection connection = url.openConnection();
			Map<String, List<String>> headerFields = connection.getHeaderFields();
			Set<Entry<String, List<String>>> entrySet = headerFields.entrySet();
			Iterator<Entry<String, List<String>>> iterator = entrySet.iterator();
			while (iterator.hasNext()) {
				Entry<String, List<String>> next = iterator.next();
				String key = next.getKey();
				List<String> value = next.getValue();
				if (key == null)
					System.out.println(value.toString());
				else
					System.out.println(key + ":" + value.toString());
			}
			System.out.println("");
		}
	} catch (IOException e) {
		System.err.println("无法查询网址!");
	}
}
 
源代码9 项目: WIFIProbe   文件: HttpRequestUtil.java
public static InputStream getStream(String url) {

        InputStream stream = null;
        try {
            String urlNameString = url;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            stream = connection.getInputStream();
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        }
        return stream;
    }
 
源代码10 项目: core   文件: WorkwaveAvlModule.java
/**
 * Access the page GET_SESSION_URL to get the cookie PHPSESSID and stores
 * the resulting cookie in sessionId member. Then when getting AVL data
 * can send the PHPSESSID cookie as authentication.
 */
private void setSessionId() {
	// Make sure the API key is configured
	if (apiKey.getValue() == null) {
		logger.error("API key not set for WorkwaveAvlModule. Needs to be "
				+ "set via Java property {} .",	apiKey.getID());
		return;
	}
	
	String fullUrl = null;
	try {
		// Create the connection
		fullUrl = GET_SESSION_URL + apiKey.getValue();
		URL url = new URL(fullUrl);
		URLConnection con = url.openConnection();
		
		// Set the timeout so don't wait forever
		int timeoutMsec = AvlConfig.getAvlFeedTimeoutInMSecs();
		con.setConnectTimeout(timeoutMsec);
		con.setReadTimeout(timeoutMsec);
		
		// Get the PHPSESSID cookie to use as the session ID
		Map<String, List<String>> headerFields = con.getHeaderFields();
		List<String> cookies = headerFields.get("Set-Cookie");
		for (String cookie : cookies) {
			if (cookie.contains("PHPSESSID")) {
				int equalSign = cookie.indexOf('=');
				int semicolon = cookie.indexOf(';');
				sessionId = cookie.substring(equalSign + 1, semicolon);
				
				logger.info("Session ID for Workwave AVL feed is "
						+ "PHPSESSID={}", sessionId);
			}
		}
	} catch (Exception e) {
		logger.error("Could not access URL {}.", fullUrl, e);
	}
}
 
源代码11 项目: Connect-SDK-Android-Core   文件: SSDPDevice.java
public void parse(URL url) throws IOException, ParserConfigurationException, SAXException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser;

    SSDPDeviceDescriptionParser parser = new SSDPDeviceDescriptionParser(this);

    URLConnection urlConnection = url.openConnection();

    applicationURL = urlConnection.getHeaderField("Application-URL");
    if (applicationURL != null && !applicationURL.substring(applicationURL.length() - 1).equals("/")) {
        applicationURL = applicationURL.concat("/");
    }

    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
    Scanner s = null;
    try {
        s = new Scanner(in).useDelimiter("\\A");
        locationXML = s.hasNext() ? s.next() : "";

        saxParser = factory.newSAXParser();
        saxParser.parse(new ByteArrayInputStream(locationXML.getBytes()), parser);
    } finally {
        in.close();
        if (s != null)
            s.close();
    }

    headers = urlConnection.getHeaderFields();
}
 
源代码12 项目: web-flash   文件: HttpUtil.java
/**
 * 向指定URL发送GET方法的请求
 *
 * @param url 发送请求的URL
 * @param param 请求参数
 * @return URL 所代表远程资源的响应结果
 */
public static String sendGet(String url, Map<String, String> param) {
    String result = "";
    BufferedReader in = null;
    try {
        StringBuffer query = new StringBuffer();

        for (Map.Entry<String, String> kv : param.entrySet()) {
            query.append(URLEncoder.encode(kv.getKey(), "UTF-8") + "=");
            query.append(URLEncoder.encode(kv.getValue(), "UTF-8") + "&");
        }
        if (query.lastIndexOf("&") > 0) {
            query.deleteCharAt(query.length() - 1);
        }

        String urlNameString = url + "?" + query.toString();
        URL realUrl = new URL(urlNameString);
        // 打开和URL之间的连接
        URLConnection connection = realUrl.openConnection();
        // 设置通用的请求属性
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 建立实际的连接
        connection.connect();
        // 获取所有响应头字段
        Map<String, List<String>> map = connection.getHeaderFields();
        // 遍历所有的响应头字段
        for (String key : map.keySet()) {
            System.out.println(key + "--->" + map.get(key));
        }
        // 定义 BufferedReader输入流来读取URL的响应
        in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
    } catch (Exception e) {
        System.out.println("发送GET请求出现异常!" + e);
        e.printStackTrace();
    }
    // 使用finally块来关闭输入流
    finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
    return result;
}
 
源代码13 项目: xiaoV   文件: HttpRequestUtil.java
/**
 * 向指定URL发送GET方法的请求
 *
 * @param url
 *            发送请求的URL
 * @param param
 *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
 * @return URL 所代表远程资源的响应结果
 */
public static String sendGet(String url, String param) {
	String result = "";
	BufferedReader in = null;
	try {
		String urlNameString = url + "?" + param;
		URL realUrl = new URL(urlNameString);
		// 打开和URL之间的连接
		URLConnection connection = realUrl.openConnection();
		// 设置通用的请求属性
		connection.setRequestProperty("accept", "*/*");
		connection.setRequestProperty("connection", "Keep-Alive");
		connection.setRequestProperty("user-agent",
				"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
		// 建立实际的连接
		connection.connect();
		// 获取所有响应头字段
		Map<String, List<String>> map = connection.getHeaderFields();
		// 遍历所有的响应头字段
		for (String key : map.keySet()) {
			System.out.println(key + "--->" + map.get(key));
		}
		// 定义 BufferedReader输入流来读取URL的响应
		in = new BufferedReader(new InputStreamReader(
				connection.getInputStream()));
		String line;
		while ((line = in.readLine()) != null) {
			result += line;
		}
	} catch (Exception e) {
		System.out.println("发送GET请求出现异常!" + e);
		e.printStackTrace();
	}
	// 使用finally块来关闭输入流
	finally {
		try {
			if (in != null) {
				in.close();
			}
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	}
	return result;
}
 
源代码14 项目: xiaoV   文件: HttpRequestUtil.java
/**
 * 向指定URL发送GET方法的请求
 *
 * @param url
 *            发送请求的URL
 * @param param
 *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
 * @return URL 所代表远程资源的响应结果
 */
public static String sendRestGet(String url) {
	String result = "";
	BufferedReader in = null;
	try {
		String urlNameString = url;
		URL realUrl = new URL(urlNameString);
		// 打开和URL之间的连接
		URLConnection connection = realUrl.openConnection();
		// 设置通用的请求属性
		connection.setRequestProperty("accept", "*/*");
		connection.setRequestProperty("connection", "Keep-Alive");
		connection.setRequestProperty("user-agent",
				"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
		// 建立实际的连接
		connection.connect();
		// 获取所有响应头字段
		Map<String, List<String>> map = connection.getHeaderFields();
		// 遍历所有的响应头字段
		for (String key : map.keySet()) {
			System.out.println(key + "--->" + map.get(key));
		}
		// 定义 BufferedReader输入流来读取URL的响应
		InputStream xx = connection.getInputStream();
		in = new BufferedReader(new InputStreamReader(xx));
		String line;
		while ((line = in.readLine()) != null) {
			result += line;
		}
	} catch (Exception e) {
		System.out.println("发送GET请求出现异常!" + e);
		e.printStackTrace();
	}
	// 使用finally块来关闭输入流
	finally {
		try {
			if (in != null) {
				in.close();
			}
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	}
	return result;
}
 
源代码15 项目: flash-waimai   文件: HttpKit.java
/**
 * 向指定URL发送GET方法的请求
 *
 * @param url 发送请求的URL
 * @param param 请求参数
 * @return URL 所代表远程资源的响应结果
 */
public static String sendGet(String url, Map<String, String> param) {
    String result = "";
    BufferedReader in = null;
    try {
        StringBuffer query = new StringBuffer();

        for (Map.Entry<String, String> kv : param.entrySet()) {
            query.append(URLEncoder.encode(kv.getKey(), "UTF-8") + "=");
            query.append(URLEncoder.encode(kv.getValue(), "UTF-8") + "&");
        }
        if (query.lastIndexOf("&") > 0) {
            query.deleteCharAt(query.length() - 1);
        }

        String urlNameString = url + "?" + query.toString();
        URL realUrl = new URL(urlNameString);
        // 打开和URL之间的连接
        URLConnection connection = realUrl.openConnection();
        // 设置通用的请求属性
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 建立实际的连接
        connection.connect();
        // 获取所有响应头字段
        Map<String, List<String>> map = connection.getHeaderFields();
        // 遍历所有的响应头字段
        for (String key : map.keySet()) {
            System.out.println(key + "--->" + map.get(key));
        }
        // 定义 BufferedReader输入流来读取URL的响应
        in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
    } catch (Exception e) {
        System.out.println("发送GET请求出现异常!" + e);
        e.printStackTrace();
    }
    // 使用finally块来关闭输入流
    finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
    return result;
}
 
源代码16 项目: lizzie   文件: AjaxHttpRequest.java
protected void sendSync(String content) throws IOException {
  if (sent) {
    return;
  }
  try {
    URLConnection c;
    synchronized (this) {
      c = this.connection;
    }
    if (c == null) {
      return;
    }
    sent = true;
    initConnectionRequestHeader(c);
    int istatus;
    String istatusText;
    InputStream err;
    if (c instanceof HttpURLConnection) {
      HttpURLConnection hc = (HttpURLConnection) c;
      String method = this.requestMethod == null ? DEFAULT_REQUEST_METHOD : this.requestMethod;

      method = method.toUpperCase();
      hc.setRequestMethod(method);
      if ("POST".equals(method) && content != null) {
        hc.setDoOutput(true);
        byte[] contentBytes = content.getBytes(postCharset);
        hc.setFixedLengthStreamingMode(contentBytes.length);
        OutputStream out = hc.getOutputStream();
        try {
          out.write(contentBytes);
        } finally {
          out.flush();
        }
      }
      istatus = hc.getResponseCode();
      istatusText = hc.getResponseMessage();
      err = hc.getErrorStream();
    } else {
      istatus = 0;
      istatusText = "";
      err = null;
    }
    synchronized (this) {
      this.responseHeaders = getConnectionResponseHeaders(c);
      this.responseHeadersMap = c.getHeaderFields();
    }
    this.changeState(AjaxHttpRequest.STATE_LOADED, istatus, istatusText, null);
    InputStream in = err == null ? c.getInputStream() : err;
    int contentLength = c.getContentLength();

    this.changeState(AjaxHttpRequest.STATE_INTERACTIVE, istatus, istatusText, null);
    byte[] bytes = loadStream(in, contentLength == -1 ? 4096 : contentLength);
    this.changeState(AjaxHttpRequest.STATE_COMPLETE, istatus, istatusText, bytes);
  } finally {
    synchronized (this) {
      this.connection = null;
      sent = false;
    }
  }
}
 
源代码17 项目: netbeans   文件: NetworkAccess.java
private SizedConnection createCallableNetwork (final URL url, final int timeout) {
    return new SizedConnection () {
        private int contentLength = -1;

        @Override
        public int getContentLength() {
            return contentLength;
        }

        @Override
        public InputStream call () throws Exception {
            URLConnection conn = url.openConnection ();
            configureConnection(conn, timeout);

            // handle redirection here
            int redirCount = 0;
            URLConnection redir = conn;
            do {
               conn = redir;
               redir = checkRedirect(conn, timeout);
               redirCount++;
            } while (conn != redir && redirCount <= MAX_REDIRECTS);

            if (conn != redir) {
                throw new IOException("Too many redirects for " + url);
            }

            InputStream is = conn.getInputStream ();
            contentLength = conn.getContentLength();
            if (err.isLoggable(Level.FINE)) {
                Map <String, List <String>> map = conn.getHeaderFields();
                StringBuilder sb = new StringBuilder("Connection opened for:\n");
                sb.append("    Url: ").append(conn.getURL()).append("\n");
                for(String field : map.keySet()) {
                   sb.append("    ").append(field==null ? "Status" : field).append(": ").append(map.get(field)).append("\n");
                }
                sb.append("\n");
                err.log(Level.FINE, sb.toString());
            }
            return new BufferedInputStream (is);
        }
    };
}
 
源代码18 项目: SwingBox   文件: BrowserPane.java
/**
 * Handle URL connection properties (most notably, content type).
 */
private void handleConnectionProperties(URLConnection conn)
{
    if (pageProperties == null)
    {
        pageProperties = new Hashtable<String, Object>(22);
    }

    String type = conn.getContentType();
    if (type != null)
    {
        // XXX mozno prepisat podla seba, setContentType, len pre text/****
        setContentType(type); // >> XXX putClientProperty("charset",
                              // charset); !!!
        // charset\s*=[\s'"]*([\-_a-zA-Z0-9]+)[\s'",;]*
        // pageProperties.put("content-type", type);
    }

    pageProperties.put(Document.StreamDescriptionProperty, conn.getURL());

    // String enc = conn.getContentEncoding();
    // if (enc != null) {
    // pageProperties.put("content-encoding", enc);
    // }

    Map<String, List<String>> header = conn.getHeaderFields();

    Set<String> keys = header.keySet();
    Object obj;
    for (String key : keys)
    {
        obj = header.get(key);
        if (key != null && obj != null)
        {
            pageProperties.put(key, obj);
        }
    }

    System.out.println("# pageProperties #");
    for (String k : pageProperties.keySet())
    {
        System.out.println(k + " : " + pageProperties.get(k));
    }

}
 
源代码19 项目: j2objc   文件: AbstractCookiesTest.java
private Map<String,List<String>> get(MockWebServer server, String path) throws Exception {
    URLConnection connection = server.getUrl(path).openConnection();
    Map<String, List<String>> headers = connection.getHeaderFields();
    connection.getInputStream().close();
    return headers;
}
 
源代码20 项目: Flashtool   文件: URLDownloader.java
public long Download(String strurl, String filedest, long seek) throws IOException {
	try {
		// Setup connection.
        URL url = new URL(strurl);
        URLConnection connection = url.openConnection();
        
        File fdest = new File(filedest);
        connection.connect();
        mDownloaded = 0;
        mFileLength = connection.getContentLength();
        strLastModified = connection.getHeaderField("Last-Modified");
        Map<String, List<String>> map = connection.getHeaderFields();

        // Setup streams and buffers.
        int chunk = 131072;
        input = new BufferedInputStream(connection.getInputStream(), chunk);
        outFile = new RandomAccessFile(fdest, "rw");
        outFile.seek(seek);
        
        byte data[] = new byte[chunk];
        LogProgress.initProgress(100);
        long i=0;
        // Download file.
        for (int count=0; (count=input.read(data, 0, chunk)) != -1; i++) { 
            outFile.write(data, 0, count);
            mDownloaded += count;
            
            LogProgress.updateProgressValue((int) (mDownloaded * 100 / mFileLength));
            if (mDownloaded >= mFileLength)
                break;
            if (canceled) break;
        }
        // Close streams.
        outFile.close();
        input.close();
        return mDownloaded;
	}
	catch (IOException ioe) {
		try {
			outFile.close();
		} catch (Exception ex1) {}
		try {
			input.close();
		} catch (Exception ex2) {}
		if (canceled) throw new IOException("Job canceled");
		throw ioe;
	}
}