• 网络编程-http连接-GET&POST


    GetRequest

    package com.net.http;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.List;
    import java.util.Map;
    
    public class GetRequest {
        
        private String url;
        private String param;
        public GetRequest(String url,String param){
            this.url=url;
            this.param=param;
        }
        
        
        public String sendRequest(){
            String result="";
            
                URL realUrl;
                try {
    realUrl
    = new URL(url);
    HttpURLConnection conn
    = (HttpURLConnection)realUrl.openConnection();
    conn.setRequestMethod(
    "GET"); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1; SV1)"); conn.setDoOutput(true); conn.setDoInput(true); try(PrintWriter out =new PrintWriter(conn.getOutputStream())){ out.println(param); out.flush(); }
    Map
    <String,List<String>> map= conn.getHeaderFields(); for(String key : map.keySet()){ System.out.println(key+ "---->"+map.get(key)); } try(BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"))){ String line; while((line=in.readLine())!=null){ result += " " + line; System.out.println(line); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } }

    test类

    package com.net.http;
    
    public class GetTest {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            new GetRequest("http://www.mocky.io/v2/574199bc120000d522a581ba","name=get").sendRequest();
        }
    
    }

    -------------------------------------------------------------------------------------------------------------------------------------------------------------

    PostRequest

    package com.net.http;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.List;
    import java.util.Map;
    
    public class PostRequest {
        
        private String result ="";
        private String url;
        private String parm;
        private String urlName;
        
        public PostRequest(String url, String parm){
            this.url=url;
            this.parm=parm;    
            if(parm==null){
                this.urlName=url;
            }else{
                this.urlName=url+"?"+ parm;
            }
            
        }
        
        public String sendRequest(){
            
            URL realUrl;
            try {
                realUrl = new URL(urlName);
    HttpURLConnection conn
    = (HttpURLConnection) realUrl.openConnection();
    conn.setRequestMethod(
    "POST"); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1; SV1)"); conn.connect(); Map<String,List<String>> map= conn.getHeaderFields(); for(String key : map.keySet()){ System.out.println(key+ "---->"+map.get(key)); } try(BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))){ String line; while((line=in.readLine())!=null){ result += " " + line; System.out.println(line); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } }

    test类

    package com.net.http;
    
    public class PostTest {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            new PostRequest("http://www.mocky.io/v2/574199bc120000d522a581ba",null).sendRequest();
        }
    
    }
  • 相关阅读:
    pytest 失败重试
    mysql 增删改查
    jenkins分布式启动slave发现ip不对应实际ip
    测试架构师必读:编码规范、开发技巧、架构画图
    mysql limit和offset用法
    pytest 使用fixture给测试函数传参
    mysql union和union all
    pytest setup和teardown用法
    pytest的Hook函数详解
    pytest 用例依赖
  • 原文地址:https://www.cnblogs.com/moonpool/p/5518775.html
Copyright © 2020-2023  润新知