JDK URLConnection示例
来源:互联网 发布:access导入sql server 编辑:程序博客网 时间:2024/06/06 00:43
代码参考Core Java II Chapter 3 Networking - Making URL Connections
发现在GET的时候,不加connect()调用也行,网上有各种讨论,但暂未找到较好的答案。
package com.example.http;import java.io.IOException;import java.io.PrintWriter;import java.net.URL;import java.net.URLConnection;import java.net.URLEncoder;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Scanner;public class Main { public static void main(String[] args) { try { testHttpGet(); testHttpPost(); } catch (IOException e) { e.printStackTrace(); } } private static void testHttpGet() throws IOException { String urlString = null; urlString = "http://www.baidu.com"; URL url = null; url = new URL(urlString); URLConnection connection = url.openConnection(); //connection.connect(); debugHeaderFields(connection); debugSomeFunctions(connection); Scanner in = null; try { in = new Scanner(connection.getInputStream()); } catch (IOException e) { e.printStackTrace(); return; } // print first 30 lines of contents for (int n = 1; in.hasNextLine() && n <= 30; n++) { System.out.println(in.nextLine()); } if (in.hasNextLine()) System.out.println(". . ."); in.close(); } private static void debugSomeFunctions(URLConnection connection) { System.out.println("Some convenience functions:"); System.out.println("----------"); System.out.println("getContentType: " + connection.getContentType()); System.out.println("getContentLength: " + connection.getContentLength()); System.out.println("getContentEncoding: " + connection.getContentEncoding()); System.out.println("getDate: " + connection.getDate()); System.out.println("getExpiration: " + connection.getExpiration()); System.out.println("getLastModifed: " + connection.getLastModified()); System.out.println("----------"); } private static void debugHeaderFields(URLConnection connection) { Map<String, List<String>> headers = connection.getHeaderFields(); System.out.println("Header fields:"); for (Map.Entry<String, List<String>> entry : headers.entrySet()) { String key = entry.getKey(); for (String value : entry.getValue()) { System.out.println(key + ": " + value); } } } private static void testHttpPost() throws IOException { String urlString = null; urlString = "http://..."; URL url = new URL(urlString); URLConnection connection = url.openConnection(); connection.setDoOutput(true); Map<String, String> nameValuePairs = new HashMap<String, String>(); nameValuePairs.put("key1", "value1"); nameValuePairs.put("key2", "value2"); //additional parameters ... PrintWriter out = new PrintWriter(connection.getOutputStream()); boolean first = true; for (Map.Entry<String, String> pair : nameValuePairs.entrySet()) { if (first) first = false; else out.print('&'); String name = pair.getKey(); String value = pair.getValue(); out.print(name); out.print('='); out.print(URLEncoder.encode(value, "UTF-8")); } out.close(); connection.connect(); debugHeaderFields(connection); debugSomeFunctions(connection); Scanner in = null; StringBuilder response = new StringBuilder(); in = new Scanner(connection.getInputStream()); while (in.hasNextLine()) { response.append(in.nextLine()); response.append("\n"); } in.close(); System.out.println("Response:"); System.out.println(response.toString()); }}
0 0
- JDK URLConnection示例
- urlconnection示例
- JDK的URLConnection详解
- JDK URLConnection 详解
- URLConnection类 示例
- URLConnection使用示例
- JDK中的URLConnection使用总结
- JDK中的URLConnection使用总结
- JDK中的URLConnection参数详解
- JDK中的URLConnection参数详解
- JDK中的URLConnection参数详解
- JDK中的URLConnection参数详解
- JDK中的URLConnection参数详解
- JDK中的URLConnection参数详解
- JDK中的URLConnection参数详解
- JDK中的URLConnection参数详解
- JDK中的URLConnection参数详解
- JDK中的URLConnection参数详解
- Iterater
- 剑指offer37_两个链表的第一个公共节点
- android的actionbar的溢出菜单默认不显示图标icon,怎样弄成微信那样可以显示icon
- ubuntu install gcc 4.1.2
- SQL 语句中 where 条件后 写上1=1 是什么意思
- JDK URLConnection示例
- 比较和排序(IComparable和IComparer以及它们的泛型实现)
- 入门两种博弈
- iOS 打包静态库放在APP上报错
- Attribute is missing the Android namespace prefix 错误小结
- 线性表/链表/队列-1
- Mysql技术内幕索引部分
- MyWidget MyLabel-- 重写 -- 双击/单击/移动/事件
- ReentrantReadWriteLock类和ReentrantLock类的区别