赵雅智_java 网络编程(4)之URL

让IE作为客户端编写服务端程序:

package net.csdn.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		ServerSocket ss = new ServerSocket(9009);
		Socket s = ss.accept();
		System.out.println(s.getInetAddress().getHostAddress()+".......conection");
		PrintWriter pwout = new PrintWriter(s.getOutputStream(),true);
		pwout.println("访问成功");
		s.close();
		//ss.close();
	}

}


 

connect 套接字:

客户端:

package net.csdn.web;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

public class ConnectClient {

	public static void main(String[] args) {
		try {
			Socket s = new Socket();
			InetSocketAddress isa = new InetSocketAddress("192.168.49.58",9001);
			s.connect(isa,5000);
			//等于Socket s = new Socket("192.168.49.58",9001));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

服务端

package net.csdn.web;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ConnectServer {
	public static void main(String[] args) throws IOException {

		ServerSocket ss = new ServerSocket(9002);
		Socket s = ss.accept();
		System.out.println(s.getInetAddress().getHostAddress()+".......conection");
		
	}

}



URL:

URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用

常用的两个构造方法:

  • URL(String spec) :根据 String 表示形式创建 URL 对象。
  • URL(URL context, String spec): 通过在指定的上下文中对给定的 spec 进行解析创建 URL。

例:

URL url = new URL("http://www.baidu.com");

相对URL对象,一般用在HTML文件中,例如:

URL urlbaidu = new URL("http://www.baidu.com");

URL urlbaidu_a = new URL(urlbaidu,"a.html");

URL urlbaidu_b = new URL(urlbaidu,"b.html");

访问URL对应的资源常用的调用方法:

  • String getFile() : 获取此 URL 的文件名。 
  • String getHost() :获取此 URL 的主机名(如果适用)。 
  • String getPath() :获取此 URL 的路径部分。 
  • int getPort() :获取此 URL 的端口号。 
  • String getProtocol() :获取此 URL 的协议名称。 
  • String getQuery() :获取此 URL 的查询部分。
  • URLConnection openConnection() :返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。 
  • InputStream openStream() :打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。

package net.csdn.web;

import java.net.MalformedURLException;
import java.net.URL;

public class UrlDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try {
			URL url = new URL("http://dianying.taobao.com/?spm=1.1000386.221827.9");
			System.out.println("protocol="+url.getProtocol());
			System.out.println("authority="+url.getAuthority());
			System.out.println("host="+url.getHost());
			System.out.println("port="+url.getPort());
			System.out.println("path="+url.getPath());
			System.out.println("query="+url.getQuery());
			System.out.println("filename="+url.getFile());
			System.out.println("ref="+url.getRef());
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}


通过URL下载网页信息

package net.csdn.web;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class TestNet3 {
	public static void main(String[] args) throws IOException {
		URL url = new URL("http://www.baidu.com/");
		InputStreamReader isr = new InputStreamReader(url.openStream());
		BufferedReader in = new BufferedReader(isr);
		String inputLine;
		FileOutputStream fos = new FileOutputStream("d:\\abc.html");
		
		while((inputLine = in.readLine())!=null){
			fos.write(inputLine.getBytes());
			System.out.println(inputLine);
		}
		in.close();
	}
}


java地址栏里的乱码转成普通字符:


URLDecoder类中有一个decode(String s, String enc)的静态方法:将乱码的特殊字符转成普通字符

URLEncoder类中有一个encode(String s, String enc)的静态方法:将普通字符转成乱码的特殊字符

package net.csdn.web;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class TestNet4 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	
	try {
		String str1 = URLEncoder.encode("java程序开发","UTF-8");
		System.out.println(str1);
		
		String str2 = URLDecoder.decode("java%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91","UTF-8");
		System.out.println(str2);
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	
	
	}

}




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值