客户端

package com.lsw.client;

import java.io.*;
import java.net.*;
import java.util.*; public class HTTPClinet {
public static void main(String[] args){
//确定http请求的uri
String uri = "index.html";
if(args.length !=0)
uri = args[0]; //按照get请求方式访问httpserver
doGet("localhost",8080,uri);
}
//按照get请求方式访问httpserver
public static void doGet(String host,int port,String uri){
Socket socket = null; try{
//与httpserver建立ftp连接
socket = new Socket(host,port);
}
catch(Exception e){
e.printStackTrace();
} try{
//创建http请求
//http请求的第一行
StringBuffer sb = new StringBuffer("GET "+uri+" HTTP/1.1\r\n");
//http请求头
sb.append("Accept: */*\r\n");
sb.append("Accept-Language: zh-cn\r\n");
sb.append("Accept-Encoding: gzip, deflate\r\n");
sb.append("User-Agent: HTTPClient\r\n");
sb.append("Host: localhost:8080\r\n");
sb.append("Connection: Keep-Alive\r\n\r\n"); //发送http请求
OutputStream socketOut = socket.getOutputStream(); //获得输出流
socketOut.write(sb.toString().getBytes()); //睡眠2秒,等待响应结果
Thread.sleep(2000); //接收响应结果
InputStream socketIn = socket.getInputStream(); //获得输入流
int size = socketIn.available();
byte[] buffer = new byte[size];
socketIn.read(buffer);
String request = new String(buffer);
System.out.println(request);
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
socket.close();
}catch(Exception e){
e.printStackTrace();
}
}
} }

服务端

package com.lsw.server;

import java.io.*;
import java.net.*; public class HTTPServer {
public static void main(String[] args){
int port;
ServerSocket serverSocket; try{
port = Integer.parseInt(args[0]);
System.out.println("默认端口是: " + port);
}
catch(Exception e){
System.out.println("默认端口8080");
port = 8080;
} try{
//创建监听端口
serverSocket = new ServerSocket(port);
System.out.println("服务器正在监听端口: " + serverSocket.getLocalPort());
while(true){
try{
//等待客户的链接请求
final Socket socket = serverSocket.accept();
System.out.println("建立了一个与客户的新的TCP连接,该客户的地址为: " + socket.getInetAddress()
+ " 端口为 : " + socket.getPort());
//响应客户请求
service(socket);
}
catch(Exception e){
e.printStackTrace();
}
} }
catch(Exception e){
e.printStackTrace();
}
} public static void service(Socket socket) throws Exception{
//读取HTTP请求信息
InputStream socketIn = socket.getInputStream(); //获得输入流
//睡眠500毫秒,等待http请求
Thread.sleep(500);
int size = socketIn.available();
byte[] buffer = new byte[size];
socketIn.read(buffer);
String request = new String(buffer);
//打印http请求数据
System.out.println("客户端请求的数据为: " +request); //解析http请求
//获得http请求第一行
String firstLineOfRequest = request.substring(0,request.indexOf("\r\n"));
System.out.println("firstLineOfRequest= " +firstLineOfRequest);
//解析http请求的第一行
String[] parts = firstLineOfRequest.split(" ");
//解析http请求这种的uri
String uri = parts[1];
System.out.println("解析http请求这种的uri=" + uri);
System.out.println("截图的值为=" + uri.substring(0, 1).toString());
String flag = uri.substring(0, 1).toString();
if(flag.equals("/")){
System.out.println("此请求是从浏览器发起的请求");
uri = uri.substring(1).toString();
} System.out.println("解析http请求这种的uri=" + uri); //决定http响应正文的类型,此处作了简化处理
String contentType;
if(uri.indexOf("html") != -1 || uri.indexOf("htm") != -1)
contentType ="text/html";
else if(uri.indexOf("jpg") != -1 || uri.indexOf("jpeg") != -1)
contentType ="image/jpeg";
else if(uri.indexOf("gif") != -1)
contentType ="image/gif";
else
contentType = "application/octet-stream"; //字节流类型 //创建http响应结果
//创建http响应的第一行
String responseFirstLine = "HTTP/1.1 200 OK\r\n";
//http响应头
String responseHeader = "Content-Type:" +contentType + "\r\n\r\n";
//获得读取响应正文数据的输入流
/*InputStream in = HTTPServer.class.getResourceAsStream(uri); 书上这么写的 哎*/
InputStream in = HTTPServer.class.getClassLoader().getResourceAsStream(uri); //发送http响应结果
OutputStream socketOut = socket.getOutputStream(); //获得输出流
//发送http响应的第一行
socketOut.write(responseFirstLine.getBytes());
//发送http响应的头
socketOut.write(responseHeader.getBytes());
System.out.println("报文头发送完成");
System.out.println("开始发送http响应的正文");
//发送http响应的正文
int len = 0;
buffer = new byte[1024];
try {
while((len=in.read(buffer)) != -1)
socketOut.write(buffer, 0, len); } catch (Exception e) {
e.printStackTrace();
}
System.out.println("完成发送http响应的正文");
//睡眠1秒,等待客户接收http响应结果
Thread.sleep(1000);
//关闭tcp连接
socket.close(); } }
<!DOCTYPE html>
<html>
<head>
<title>hello.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body>
This is my HTML page. <br>
</body>
</html>

模拟界面请求到web服务器的更多相关文章

  1. 您的请求在Web服务器中没有找到对应的站点”这是什么原因?出现这个界面说明域名解析已经正确并生效,这是由于域名没有绑定好,

    宝塔出现 您的请求在Web服务器中没有找到对应的站点"这是什么原因?出现这个界面说明域名解析已经正确并生效,这是由于域名没有绑定好 , 本人经过测试使用如下方法解决.允话空HTTP_REFE ...

  2. js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq

    js_html_input中autocomplete="off"在chrom中失效的解决办法 分享网上的2种办法: 1-可以在不需要默认填写的input框中设置 autocompl ...

  3. C#模拟httpwebrequest请求_向服务器模拟cookie发送

    使用C#代码模拟web请求,是一种常用的方法,以前没专门整理过,这里暂时贴上自己整理的完整代码,以后再做梳理: public class MyRequest { #region 辅助方法 public ...

  4. C#中使用Socket请求Web服务器过程

    最开始我们需要明白一件事情,因为这是这篇文章的前提: HTTP协议只是一个应用层协议,它底层是通过TCP进行传输数据的.因此,浏览器访问Web服务器的过程必须先有“连接建立”的发生. 而有人或许会问: ...

  5. 详谈socket请求Web服务器过程

    最开始我们需要明白一件事情,因为这是这篇文章的前提: HTTP协议只是一个应用层协议,它底层是通过TCP进行传输数据的.因此,浏览器访问Web服务器的过程必须先有“连接建立”的发生. 而有人或许会问: ...

  6. 详谈socket请求Web服务器过程(转)

    最开始我们需要明白一件事情,因为这是这篇文章的前提: HTTP协议只是一个应用层协议,它底层是通过TCP进行传输数据的.因此,浏览器访问Web服务器的过程必须先有“连接建立”的发生. 而有人或许会问: ...

  7. 【网络开发】详谈socket请求Web服务器过程

    最开始我们需要明白一件事情,因为这是这篇文章的前提: HTTP协议只是一个应用层协议,它底层是通过TCP进行传输数据的.因此,浏览器访问Web服务器的过程必须先有"连接建立"的发生 ...

  8. 自己动手模拟开发一个简单的Web服务器

    开篇:每当我们将开发好的ASP.NET网站部署到IIS服务器中,在浏览器正常浏览页面时,可曾想过Web服务器是怎么工作的,其原理是什么?“纸上得来终觉浅,绝知此事要躬行”,于是我们自己模拟一个简单的W ...

  9. 一个简单的Web服务器-支持Servlet请求

    上接 一个简单的Web服务器-支持静态资源请求,这个服务器可以处理静态资源的请求,那么如何处理Servlet请求的呢? 判断是否是Servlet请求 首先Web服务器需要判断当前请求是否是Servle ...

随机推荐

  1. myeclipse使用小结

    1.项目设置编码格式 (1)全局编码设置:编码设置的方法:ToolBar-->Window-->Preferences-->General-->Workspace-->T ...

  2. Writing your first academic paper

    Writing your first academic paper If you are working in academics (and you are if you are working wi ...

  3. html向js传递id

    html获取id方法: <div id="thediv1" style="display:block" onclick="ceshi(this. ...

  4. 如何使用Defender优雅的管理权限?

    何为权限管理 权限管理已经不知不觉深入到了我们生活的每一个角落,例如地铁进站的闸机,高速公路上的过路费,停车场的杠杆等等等等. 作为一名开发人员,权限二字对我们的映像更加深刻,无论任何系统,都多多少少 ...

  5. [NOI导刊2010提高&洛谷P1774]最接近神的人 题解(树状数组求逆序对)

    [NOI导刊2010提高&洛谷P1774]最接近神的人 Description 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某 ...

  6. TensorFlow在windows10上的安装与使用(一)

    随着近两年tensorflow越来越火,在一台新win10系统上装tensorflow并记录安装过程.华硕最近的 Geforce 940mx的机子. TensorFlow是一个采用数据流图(data ...

  7. 【leetcode 简单】 第八十三题 反转字符串中的元音字母

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leet ...

  8. 正则表达式入门之学习路线&七个问题

    由于工作需求,需要使用正则表达式查找满足某种模式的字符串,但因为之前都没有接触过相关内容,最开始的时候看了一些已经被别人写好了的正则表达式,本来打算可能可以直接使用: 最全的常用正则表达式大全——包括 ...

  9. hdu 确定比赛名次(拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    ...

  10. Shell-help格式详解

    前言 linux shell命令通常可以通过-h或--help来打印帮助说明,或者通过man命令来查看帮助,有时候我们也会给自己的程序写简单的帮助说明,其实帮助说明格式是有规律可循的 帮助示例 下面是 ...