下面列出了java.net.URLConnection#getHeaderFields ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 根据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;
}
@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());
}
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;
}
/**
* 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");*/
}
/**
* 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");*/
}
/**
* 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;
}
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;
}
/**
* 获取响应头
* 打印到控制台
*/
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("无法查询网址!");
}
}
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;
}
/**
* 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);
}
}
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();
}
/**
* 向指定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;
}
/**
* 向指定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;
}
/**
* 向指定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;
}
/**
* 向指定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;
}
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;
}
}
}
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);
}
};
}
/**
* 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));
}
}
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;
}
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;
}
}