当前位置:   article > 正文

让浏览器可以访问我们的socket服务 (http协议入门)_浏览器 通过socket调用本地服务

浏览器 通过socket调用本地服务
在我们的web开发中,会是用到web服务器,比如:tomcat、apache等。
其实它们就是功能做的很强大的socket服务。
我们使用的数据库程序,包括一些和网络相关的软件都和我们的socket是有关系的。
这中间产生的交互,很可能是通过某种协议来完成的。
只不过有些协议好理解(http协议),有些软件专有的协议可能不太好理解。
  • 1
  • 2
  • 3
  • 4
  • 5

当我们在浏览器地址栏输入对应的IP地址,其实也就是浏览器创建了一个socket连接。那么服务端能否响应一段文字呢?

1、我们先来创建一个Request类,用于处于http的请求。
Request.java:

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;


/**
 * http请求 接收类
 */
public class Request {

    // 用来保存输入流对象
    private InputStream inputStream;
    // 从输入流中每次读取的字节长度
    private int bufferLength = 1024;

    /**
     * 构造函数
     * @param inputStream 输入流对象
     */
    public Request(InputStream inputStream){
        this.inputStream = inputStream;
    }

    /**
     * 读取内容
     * @return 返回的是字符串
     * @throws IOException
     */
    public String readHtml() throws IOException {
        // 使用BufferedInputStream对象来读取
        BufferedInputStream bufferedInputStream = new BufferedInputStream(this.inputStream);
        // 每次读取1024字节
        byte[] buffer = new byte[this.bufferLength];

        // 字符串拼接对象
        StringBuilder stringBuilder = new StringBuilder();
        int length = 0;
        while (true){

            length = bufferedInputStream.read(buffer);

            // 把读取的字节转为String类型
            // 然后拼接成一个大的字符串
            stringBuilder.append(new String(buffer,0,length));

            if (length<=0 || length<this.bufferLength){
                break; // 如果没有读取到内容就跳出循环
            }


        }

        // 返回字符串内容
        return stringBuilder.toString();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

这个Request类有2个方法:构造方法接受一个InputStream对象(socket中的输入流对象),readHtml()方法是用来读去到底浏览器给我们发送了什么内容(从inputStream对象中获取内容)。

2、然后我们就可以在socket服务端使用Request类
MyServer.java:

    public static void main(String[] args) throws IOException {
        // 服务端创建一个监听
        ServerSocket serverSocket = new ServerSocket(9000);
        // 监听客户端连接,这里我们暂时没有多线程
        Socket socket = serverSocket.accept();

        // 要想获取客户端发送过来的内容,就要得到 InputStream类 的输入流对象
        InputStream inputStream = socket.getInputStream();

        //==========使用我们自定义的http处理的相关类===========//
        Request request = new Request(inputStream);
        System.out.println(request.readHtml());

        socket.close();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

3、运行MyServer.java,也就是运行socket服务端。然后浏览器访问http://localhost:9000
我们在服务端打印了Request对象读取到的内容,我们可以在控制台看见:

GET / HTTP/1.1
Host: localhost:9000
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Upgrade-Insecure-Requests: 1
Cookie: Webstorm-b70439c5=403d1adc-2a8d-41c9-936a-1bf77c1ee41f
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
Connection: keep-alive
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这就是一个http发送请求的数据。

4、服务端能够获取到浏览器的输入了,我们尝试给浏览器一个响应,比如响应一个字符串this is server
我创建一个http响应类Response.java


import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Response {

    private OutputStream outputStream;

    public Response(OutputStream outputStream){
        this.outputStream = outputStream;
    }

    public void writeHtml(String html) throws IOException{
        // 拼装http响应的数据格式
        html = "http/1.1 200 ok\n"
                +"\n\n"
                +html;


        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(this.outputStream);
        bufferedOutputStream.write(html.getBytes());
        bufferedOutputStream.close();
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

可以看出writeHtml()方法中,拼装了一个符合http协议响应格式的字符串(符合http协议才会被浏览器正常解析识别)。

5、来到我们的socket服务端代码,现在既要处理输入也要处理输出了
MyServer.java:

    public static void main(String[] args) throws IOException {
        // 服务端创建一个监听
        ServerSocket serverSocket = new ServerSocket(9000);
        // 监听客户端连接,这里我们暂时没有多线程
        Socket socket = serverSocket.accept();

        // 要想获取客户端发送过来的内容,就要得到 InputStream类 的输入流对象
        InputStream inputStream = socket.getInputStream();

        //==========使用我们自定义的http处理的相关类===========//
        Request request = new Request(inputStream);
        System.out.println(request.readHtml());

        // 要想给客户端发送数据,就要得到
        OutputStream outputStream = socket.getOutputStream();
        Response response = new Response(outputStream);
        response.writeHtml("this is server");

        // 关闭
        inputStream.close();
        outputStream.close();
        socket.close();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

现在浏览器访问http://localhost:9000的话,页上给我们显示this is server这样一个字符串了。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号