首页 > 其他分享 >使用socket 和 httpURLConnection发起http请求

使用socket 和 httpURLConnection发起http请求

时间:2023-03-15 11:58:54浏览次数:38  
标签:count http socket buffer bw sb new httpURLConnection

  public static void test() throws Exception {
//        http://127.0.0.1:8080/logger/user
        InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
        Socket socket = new Socket(inetAddress.getHostAddress(), 80);

        if (socket.isConnected()) {
            System.out.println("连接建立,远程地址:" + socket.getRemoteSocketAddress());
        }

        // 关键!此处在Socket的输出流写入HTTP的GET报文,请服务器做出响应。
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("GET / HTTP/1.1\r\n");
        bw.write("Host: www.baidu.com\r\n");
        bw.write("\r\n");
        bw.flush();

        // 开始读取远程服务器的响应数据。
        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());

        byte[] buffer = new byte[1024];
        int count = 0;
        StringBuilder sb = new StringBuilder();
        while (true) {
            count = bis.read(buffer);
            if (count == -1) {
                break;
            }

            sb.append(new String(buffer, 0, count, "UTF-8"));
        }

        System.out.printf("sb:"+sb.toString());
        bw.close();
        bis.close();
        socket.close();
    }


    public static void test2() throws Exception {
        URL url = new URL("http://127.0.0.1:8080/logger/user");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        // 开始读取远程服务器的响应数据。
        BufferedInputStream bis = new BufferedInputStream(urlConnection.getInputStream());
        StringBuilder sb = new StringBuilder();
        byte[] buffer = new byte[1024 * 10];
        int count = 0;
        while (true) {
            count = bis.read(buffer);
            if (count == -1) {
                break;
            }

            sb.append(new String(buffer, 0, count, "UTF-8"));
        }
        System.out.printf(""+sb.toString());
        bis.close();
    }

 

标签:count,http,socket,buffer,bw,sb,new,httpURLConnection
From: https://www.cnblogs.com/niun/p/17217970.html

相关文章

  • HTTP方法
    常用的HTTP方法浏览器发送请求时采用的方法,和响应无关GET、POST、PUT、DELECT用来定义对于资源采取什么样的操作的,有各自的语义HTTP方法的语义增删改查GET获取数据:......
  • 4 HttpServletRequest
    ​ HttpServletRequestHttpServletRequest对象代表客户端浏览器的请求,当客户端浏览器通过HTTP协议访问服务器时,HTTP请求中的所有信息都会被Tomcat所解析并封装在这个对......
  • 4 HttpServletRequest
    ​ HttpServletRequestHttpServletRequest对象代表客户端浏览器的请求,当客户端浏览器通过HTTP协议访问服务器时,HTTP请求中的所有信息都会被Tomcat所解析并封装在这个对......
  • 初识HTTP、HTTP报文
    初识HTTP1、HTTP是什么HyperTextTransferProtocol超文本传输协议HTML:超文本标记语言超文本:原先一个个单一的文本,通过超链接将其联系起来,由原先的单一的文本变成了......
  • HttpClientUtil
    <!--http请求工具包依赖--><dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version></depende......
  • delphi httpserver 使用方法
    unitmain;interfaceuses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms, Dialogs,IdBaseComponent,IdContext,IdComponent,IdCusto......
  • 以下是一个使用C++实现HTTP文件下载的简单示例,其中使用了C++ 11的标准库和Boost库:
    #include<iostream>#include<fstream>#include<boost/asio.hpp>usingboost::asio::ip::tcp;intmain(){try{boost::asio::io_serviceio_se......
  • XMLHttpRequest下载文件
    //表单数据varformData=newFormData();formData.append("whereStr","aaa");formData.append("orderStr","bbb");//打开加载loading框parent.$.messager.progre......
  • C# HttpClient 的使用
    介绍个HTTPClient中常用于记录请求以及响应的中间建:System.Net.Http.DelegatingHandler文档,不废话直接看代码声明:全篇基于.NET7编写点击查看代码publicseal......
  • HTTP协议
    HTTP协议目录HTTP协议1、常见HTTP客户端思考1、网络协议为什么要分层?2、www包含了哪些技术?3、http请求/响应报文包含了哪些内容?4、http特点有哪些?2、代理的作用1、常见HT......