009socket基本使用

    技术2022-07-11  105

    package sock1; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class PathCon { public static void main(String[] args) throws Exception { String str = "http://127.0.0.1:8080/mypac/abc.html"; str = "http://www.baidu.com:8080/mypac/abc.html?name=tom&age=21"; str = "http://www.baidu.com/mypac/abc.html?name=tom&age=21"; URL url = new URL(str); System.out.println("url.getProtocol() = " + url.getProtocol()); System.out.println("url.getHost() = " + url.getHost()); int port = url.getPort(); if (port == -1) { port=80; } System.out.println("url.getPort() = " + port); System.out.println("url.getPath() = " + url.getPath()); System.out.println("url.getQuery() = " + url.getQuery()); System.out.println("url.getFile() = " + url.getFile()); } } class Connect{ public static void main(String[] args) throws Exception { String str = "http://127.0.0.1:8080/mypac/abc.html"; URL url = new URL(str); URLConnection urlConnection = url.openConnection(); System.out.println("urlConnection = " + urlConnection); InputStream inputStream = urlConnection.getInputStream(); // OutputStream outputStream = urlConnection.getOutputStream(); // PrintWriter printWriter = new PrintWriter(outputStream, true); // printWriter.println(""); byte[] arr = new byte[1234]; int read = inputStream.read(arr); System.out.println(new String(arr,0,read,"gbk")); } }
    Processed: 0.013, SQL: 9