JAVA java.net库发送get/post请求基础

1.get

package jiekou_test_test2;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;


public class juhewether_get {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        // set fillder proxy
        System.setProperty("http.proxySet", "true"); 
        System.setProperty("http.proxyHost", "127.0.0.1"); 
        System.setProperty("http.proxyPort", "8888");
        
        String lon = "116";
        String lat = "39";
        String key = "bf74bdf2807069fd35a82e3aa19445d5";
        String url = "http://v.juhe.cn/weather/geo"+"?"+"lon="+lon+"&"+"lat="+lat+"&"+"key="+key;
        
        try {
            
            URL juhe_wether_get_url = new URL(url);
            HttpURLConnection openConnection = (HttpURLConnection)juhe_wether_get_url.openConnection();
            
            openConnection.connect();
            
            
            InputStream is = openConnection.getInputStream();
            InputStreamReader ir = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(ir);
            
            
            StringBuffer sb = new StringBuffer();
            String temp = "";
            
            while((temp = br.readLine()) != null) {
                sb.append(temp);
            }
            
            String result = sb.toString();
            
            
            System.out.print(result);
            
            
        }catch(Exception e){
            
        }finally {
            
        }
        
        
        
        
    }

}

2.POST

package jiekou_test_test2;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;


public class juhewether_post {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        //set fiddler proxy
        System.setProperty("http.proxySet", "true"); 
        System.setProperty("http.proxyHost", "127.0.0.1"); 
        System.setProperty("http.proxyPort", "8888");
        
        String lon = "116";
        String lat = "39";
        String key = "bf74bdf2807069fd35a82e3aa19445d5";
        String url = "http://v.juhe.cn/weather/geo";
        
        //create a url
        URL post_url = null;
        
        HttpURLConnection openConnection = null;
        
        try {
            
            
            post_url = new URL(url);
            
            // open collection
            openConnection = (HttpURLConnection)post_url.openConnection();
            
            // set request method
            openConnection.setRequestMethod("POST");
            
            // set headers
            openConnection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            
            // preapare  data
            String data = "lon="+lon+"&"+"lat="+lat+"&"+"key="+key;
            
            // open write os
            openConnection.setDoOutput(true);        
            
            //write data
            OutputStream os = openConnection.getOutputStream();
            os.write(data.getBytes());
            
            
            if(openConnection.getResponseCode() == 200) {
                
                // read inputstream
                InputStream is = openConnection.getInputStream();
                InputStreamReader isr = new InputStreamReader(is, "UTF-8");
                
                BufferedReader br = new BufferedReader(isr);
                
                
                
                //strore result 
                StringBuffer sb = new StringBuffer();
                String temp = "";
                
                while((temp=br.readLine()) != null) {
                    
                    sb.append(temp);
                    
                }
                
                String result = sb.toString();
                
                System.out.print(result);
                
            }else {
                
                System.out.print("status code is not 200!");
                
            }
            
            
            
                    }
        
        
        }catch(Exception e) {
            
            e.printStackTrace();
            System.out.print("exception occurs");
            
        }finally{
            
            if(openConnection != null) {
                openConnection.disconnect();
            }
            


    }

}

3.记忆点

  1.需要知道request的面向对象的设计思想,记住需要使用的类:

    reqeust包:

    URL

    HttpURLConnection

    IO流

  2.真正发送请求的时机是 httpURLConnection.getInputStream方法的调用(通过fiddler抓包证实)

  3.httpURLConnection.connect()方法不需要显示地调用,它的作用是建立tcp连接,如果不显示的调用,从connection获取输入流的时候也会先建立连接。

  它更多的作用是区别设置request和获取response的时机。

  4.参考博文:https://www.cnblogs.com/skys-li/p/6121044.html

  

猜你喜欢

转载自www.cnblogs.com/shenwazaishenwa/p/11436606.html
今日推荐