java041:java或浏览器做客户端互相传递数据

一,用java做服务器,用浏览器做客户端,让浏览器去链接java服务器

每一个浏览器在发送请求时都会先发送一个”暗号”,对方如果能对上这个暗号,就是自己人,才会发送数据。 这个暗号就是我们的http协议
用java做客户端,tomcat做服务器:

package 网络编程;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
	public static void main(String[] args) throws UnknownHostException, IOException {
		System.out.println("我执行了没有");
		Socket tomcat=new Socket("localhost",8080);
		System.out.println("我执行了没");
		//接收信息
		InputStream in=tomcat.getInputStream();
		InputStreamReader isr=new InputStreamReader(in);
		BufferedReader br=new BufferedReader(isr);
		//发送暗号
		OutputStream out=tomcat.getOutputStream();
		OutputStreamWriter osw=new OutputStreamWriter(out);
		BufferedWriter bw=new BufferedWriter(osw);
        //暗号,模仿http协议
		 bw.write("GET /baidu/1.html HTTP/1.1");
		 bw.newLine();
		 bw.write("Host: localhost:8080");
		 bw.newLine();
		 bw.write("Connection: close");
		 bw.newLine();
		 bw.write("Cache-Control: max-age=0");
		 bw.newLine();
		 bw.write("Upgrade-Insecure-Requests: 1");
		 bw.newLine();
		 bw.write("User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");
		 bw.newLine();
		 bw.write("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
		 bw.newLine();
		 bw.write("Accept-Encoding: gzip, deflate, br");
		 bw.newLine();
		 bw.write("Accept-Language: zh-CN,zh;q=0.9,en;q=0.8");
		 bw.newLine();
		 bw.write("Cookie: Webstorm-9770a2b9=e9c5983e-de71-494b-add2-1f771d40a956");
		 bw.newLine();
		 bw.newLine(); //注意协议后面多了两个换行,这也是协议内容。
		 bw.newLine();
		 bw.flush();
		//接收
		String msg=null;
		while((msg=br.readLine())!=null){
			System.out.println(msg);
		}
		System.out.println("over");
		br.close();
		

	}

}

java041:java或浏览器做客户端互相传递数据
出现这种情况,应该是你Tomcat的服务端口被占用了。不知道怎么办了。
**二,用浏览器做客户端,用java做服务器,给浏览器发送html代码,让浏览器显示页面

package 网络编程;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	public static void main(String[] args) throws IOException {
		ServerSocket server=new ServerSocket(8888);
		System.out.println("服务器以启动。。");
		Socket llq=server.accept();
		System.out.println("有客户端接入");
		OutputStream out=llq.getOutputStream();
		OutputStreamWriter osw=new OutputStreamWriter(out);
		BufferedWriter bw=new BufferedWriter(osw);
		
		bw.write("HTTP/1.1 200 OK");
		bw.newLine();
		bw.write("Server: Apache-Coyote/1.1");
		bw.newLine();
		bw.write("ETag: W/\"7347-1184876416000\"");
		bw.newLine();
		bw.write("Last-Modified: Thu, 19 Jul 2007 20:20:16 GMT");
		bw.newLine();
		bw.write("Content-Type: text/html");
		bw.newLine();
		bw.write("Content-Length: 7347");
		bw.newLine();
		bw.write("Date: Sat, 11 May 2019 08:22:31 GMT");
		bw.newLine();
		bw.newLine();
		bw.newLine();
		
		bw.flush();
		
		bw.write("<div style='border:1px solid red; width:100px; height:100px'></div>");
		bw.flush();
		bw.close();
		System.out.println("over");

	}

}

输出结果:在网页上显示:
java041:java或浏览器做客户端互相传递数据

上一篇:使用Socket简单模拟C/S消息传递——Java(31)


下一篇:通过java 来实现对多个文件的内容合并到一个文件中