zoukankan      html  css  js  c++  java
  • 网络编程-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();
        }
    
    }
  • 相关阅读:
    【题解】JSOI2009游戏
    【考试记录】4.8 Path (网络流 —— 劲题)
    【考试记录】4.8 Table ( 数论数学 --组合数 & 杨辉三角)
    【题解】HNOI2016树
    【算法】最小乘积生成树 & 最小乘积匹配 (HNOI2014画框)
    【加油!】
    [bzoj4916] 神犇和蒟蒻 [杜教筛]
    [CQOI2015][bzoj3930] 选数 [杜教筛+莫比乌斯反演]
    [luogu3768] 简单的数学题 [杜教筛]
    春季学习记录
  • 原文地址:https://www.cnblogs.com/moonpool/p/5518775.html
Copyright © 2011-2022 走看看