URL https://i.cnblogs.com/EditPosts.aspx?opt=1

URL url = new URL("https://i.cnblogs.com");
URL url1 = new URL(url, "EditPosts.aspx?opt=1");
System.out.println(url);//https://i.cnblogs.com
System.out.println(url1);//https://i.cnblogs.com/EditPosts.aspx?opt=1
System.out.println("URL主机名称:"+url1.getHost());//URL主机名称:i.cnblogs.com
System.out.println("URL协议:"+url1.getProtocol());//URL协议:https
System.out.println("URL端口:"+url1.getPort());//URL端口:-1
System.out.println("URL文件路径:"+url1.getPath());//URL文件路径:/iEditPosts.aspx
System.out.println("URL查询字符串:"+url1.getQuery());//URL查询字符串:opt=1
System.out.println("URL相对路径:"+url1.getRef());//URL相对路径:null
System.out.println("URL文件名:"+url1.getFile());//URL文件名:/EditPosts.aspx?opt=1
try {
//创建一个URL实例
URL url = new URL("http://www.baidu.com");
//通过URL对象的openstream方法获取一个InputStream对象
InputStream is = url.openStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
File bd = new File(".../index.jsp");
PrintWriter pw = new PrintWriter(bd);
//按行读取 获取
access_token

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxd65107df4acf0a81&secret=1cc78b3caf6f7e509dbffe710b1e1cd9

        String urlNameString = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxd65107df4acf0a81&secret=1cc78b3caf6f7e509dbffe710b1e1cd9";
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的响应
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
String result ="";
while ((line = in.readLine()) != null) {
result += line;
}
if (in != null) {
in.close();
}
return result; /** result 返回的json串

{
"access_token": "19_pNVNyZVX5WgtPdFarp5epawu7S-0hJTv1nvQuDJTjcUaCS_gotVa_7_-eoLhPgs2K2eKZRXuEtuONJYrW4SH3qFKhLl1-QApZ57ZqtapADJcwD6uREzmEusulyUTPHjAEAWSK",
"expires_in": 7200
}

*/

access_token
;
    String s = br.readLine();
while(s!=null){
pw.print(s);
pw.flush();
s=br.readLine();
}
//关闭相关资源
pw.close();
br.close();
isr.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
上一篇:jcarousellite 实现图片列表滚动


下一篇:js控制全屏及退出全屏