本文整理汇总了Java中java.net.URLConnection.getHeaderFields方法的典型用法代码示例。如果您正苦于以下问题:Java URLConnection.getHeaderFields方法的具体用法?Java URLConnection.getHeaderFields怎么用?Java URLConnection.getHeaderFields使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.URLConnection
的用法示例。
在下文中一共展示了URLConnection.getHeaderFields方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test01
import java.net.URLConnection; //导入方法依赖的package包/类
/**
* Set-Cookie:xqat=93b9123bccf67168e3adb0c07d89b9e1f6cc8db6; domain=.xueqiu.com; path=/; expires=Mon, 23 May 2016 13:36:54 GMT; httpOnly
Set-Cookie:xq_r_token=8cfa9fd958be66a0a6692ab80219e8eaecef6718; domain=.xueqiu.com; path=/; expires=Mon, 23 May 2016 13:36:54 GMT; httpOnly
Set-Cookie:xq_is_login=1; domain=xueqiu.com; path=/; expires=Mon, 23 May 2016 13:36:54 GMT
Set-Cookie:u=2970459786; domain=.xueqiu.com; path=/; expires=Mon, 23 May 2016 13:36:54 GMT; httpOnly
Set-Cookie:xq_token_expire=Mon%20May%2023%202016%2021%3A36%3A54%20GMT%2B0800%20(CST); domain=.xueqiu.com; path=/; expires=Mon, 23 May 2016 13:36:54 GMT; httpOnly
Set-Cookie:xq_a_token=93b9123bccf67168e3adb0c07d89b9e1f6cc8db6; domain=.xueqiu.com; path=/; expires=Mon, 23 May 2016 13:36:54 GMT; httpOnlyvv
* @throws Exception
*/
@Test
public void test01() throws Exception {
String base = "https://xueqiu.com/user/login";
String param = "areacode=86&username=jt120lz%40gmail.com&password=3E0409E11A5756B971C54CC38035C198&remember_me=on";
URL url = new URL(base+"?"+param);
URLConnection conn = url.openConnection();
conn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String s = null;
while ((s = br.readLine()) != null) {
System.out.println(s);
}
br.close();
Map<String, List<String>> headers = conn.getHeaderFields();
headers.forEach((key,value)-> {
if (key.startsWith("Set-Cookie")) {
value.forEach(v->System.out.println(v));
}
});
}
示例2: unshortenUrl
import java.net.URLConnection; //导入方法依赖的package包/类
/**
* Unshortens a short url
*/
private String unshortenUrl(String shortURL) {
if (!shortURL.startsWith(HTTP_PROTOCOL)
&& !shortURL.startsWith(HTTPS_PROTOCOL))
return "";
URLConnection urlConn = connectURL(shortURL);
urlConn.getHeaderFields();
String finalResult = urlConn.getURL().toString();
urlConn = connectURL(finalResult);
urlConn.getHeaderFields();
shortURL = urlConn.getURL().toString();
while (!shortURL.equals(finalResult)) {
finalResult = unshortenUrl(finalResult);
}
return finalResult;
}
示例3: getStream
import java.net.URLConnection; //导入方法依赖的package包/类
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;
}
示例4: sendGet
import java.net.URLConnection; //导入方法依赖的package包/类
@Test
public void sendGet() throws Exception {
URLConnection connection = HttpTools.sendGet("http://192.168.31.1");
Map<String, List<String>> map = connection.getHeaderFields();
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
}
示例5: storeCookies
import java.net.URLConnection; //导入方法依赖的package包/类
private static void storeCookies(URLConnection urlConnection) {
Map<String, List<String>> headerFields = urlConnection.getHeaderFields();
List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);
if (cookiesHeader == null)
return;
lastCookies = new CookieManager();
for (String cookie : cookiesHeader)
lastCookies.getCookieStore().add(null, HttpCookie.parse(cookie).get(0));
}
示例6: createCallableNetwork
import java.net.URLConnection; //导入方法依赖的package包/类
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 ();
conn.setConnectTimeout (timeout);
conn.setReadTimeout(timeout);
if(conn instanceof HttpsURLConnection){
NetworkAccess.initSSL((HttpsURLConnection) conn);
}
// 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);
}
};
}
示例7: invoke
import java.net.URLConnection; //导入方法依赖的package包/类
public PropBagEx invoke(BlackBoardSessionData data, String servlet, String name, Collection<NameValue> parameters)
throws IOException
{
long startTime = System.currentTimeMillis();
BlindSSLSocketFactory.register();
// Setup URLConnection to appropriate servlet/jsp
URLConnection con = new URL(this.url, servlet).openConnection();
con.setConnectTimeout(TIMEOUT);
con.setReadTimeout(TIMEOUT);
con.setDoInput(true);
con.setDoOutput(true);
String token = data.getBlackBoardSession();
// BB7 contains '@@'
final int expectedTokenLength = PASS_LENGTH + (token.startsWith("@@") ? 2 : 0);
if( token.length() == expectedTokenLength )
{
con.setRequestProperty("Cookie", "session_id=" /* @@" */+ token + ";");
}
// Open output stream and send username and password
PrintWriter conout = new PrintWriter(con.getOutputStream());
StringBuilder out = new StringBuilder();
out.append("method=" + name + "&");
if( parameters != null )
{
for( NameValue pair : parameters )
{
out.append(pair.getValue() + "=" + encode(pair.getName()) + "&");
}
}
conout.print(out.toString());
conout.close();
InputStream in = con.getInputStream();
PropBagEx xml = parseInputStream(in);
String cookie = con.getHeaderField("Set-Cookie");
if( cookie == null )
{
Map<String, List<String>> headerFields = con.getHeaderFields();
if( headerFields != null && !Check.isEmpty(headerFields.get("Set-Cookie")) )
{
cookie = headerFields.get("Set-Cookie").get(0);
}
}
xml.setNode("cookie", cookie);
in.close();
int buildingBlockDuration = xml.getIntNode("@invocationDuration", -1);
int thisMethodDuration = (int) ((System.currentTimeMillis() - startTime) / 1000);
StringBuilder sb = new StringBuilder("URL request from EQUELLA to Blackboard took ");
sb.append(thisMethodDuration);
sb.append(" second(s), where ");
sb.append(buildingBlockDuration);
sb.append(" second(s) where spent in the Building Block");
LOGGER.info(sb.toString());
return xml;
}
示例8: retreiveMetadata
import java.net.URLConnection; //导入方法依赖的package包/类
synchronized private void retreiveMetadata() throws IOException {
URLConnection con = streamUrl.openConnection();
con.setRequestProperty("Icy-MetaData", "1");
con.setRequestProperty("Connection", "close");
con.setRequestProperty("Accept", null);
con.connect();
int metaDataOffset = 0;
Map<String, List<String>> headers = con.getHeaderFields();
InputStream stream = con.getInputStream();
if (headers.containsKey("icy-metaint")) {
// Headers are sent via HTTP
metaDataOffset = Integer.parseInt(headers.get("icy-metaint").get(0));
} else {
// Headers are sent within a stream
StringBuilder strHeaders = new StringBuilder();
char c;
while ((c = (char) stream.read()) != -1) {
strHeaders.append(c);
if (strHeaders.length() > 5 && (strHeaders.substring((strHeaders.length() - 4), strHeaders.length()).equals("\r\n\r\n"))) {
// end of headers
break;
}
}
// Match headers to get metadata offset within a stream
Pattern p = Pattern.compile("\\r\\n(icy-metaint):\\s*(.*)\\r\\n");
Matcher m = p.matcher(strHeaders.toString());
if (m.find()) {
metaDataOffset = Integer.parseInt(m.group(2));
}
}
// In case no data was sent
if (metaDataOffset == 0) {
isError = true;
return;
}
// Read metadata
int b;
int count = 0;
int metaDataLength = 4080; // 4080 is the max length
boolean inData = false;
StringBuilder metaData = new StringBuilder();
// Stream position should be either at the beginning or right after headers
while ((b = stream.read()) != -1) {
count++;
// Length of the metadata
if (count == metaDataOffset + 1) {
metaDataLength = b * 16;
}
if (count > metaDataOffset + 1 && count < (metaDataOffset + metaDataLength)) {
inData = true;
} else {
inData = false;
}
if (inData) {
if (b != 0) {
metaData.append((char) b);
}
}
if (count > (metaDataOffset + metaDataLength)) {
break;
}
}
// Set the data
metadata = IcyStreamMeta.parseMetadata(metaData.toString());
// Close
stream.close();
}