本文主要是介绍JDK URLConnection示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码参考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 contentsfor (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());}}
这篇关于JDK URLConnection示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!