1. 程式人生 > >java 後臺模擬post表單請求傳送key-value資料亂碼

java 後臺模擬post表單請求傳送key-value資料亂碼

首先是key-value的模型:

public class PostValue {
  
    private String key;  
   
  
    private String value;  
  
    public PostValue(String key, String value)  
  
    {  
  
        this.key = key;  
  
        this.value = value;  
  
    }  
  
    public String getKey()  
  
    {  
  
        return key;  
  
    }  
  
    public void setKey(String key) {  
  
        this.key = key;  
  
    }  
  
    public String getValue()  
  
    {  
  
        return value;  
  
    }  
  
    public void setValue(String value)  
  
    {  
  
        this.value = value;  
  
    }  


}

然後是postutils :

public class PostUtils {


public static String sendPost(String url,List<PostValue> parameters ) {   
        String result = "";// 返回的結果   
        BufferedReader in = null;// 讀取響應輸入流   
        PrintWriter out = null;  //輸出流 
        String params = "";// 編碼之後的引數   
        try {   
            // 編碼請求引數   
            if(parameters.size() == 1){//請求只有一個  
                for(PostValue p:parameters){ 
                String s=p.getKey()+"="+p.getValue();
                    System.out.println(1);
                    params=s;
                }  
            }else{  
            String x=null;
                for(PostValue p:parameters){  
                x+=p.getKey()+"="+p.getValue()+"&";
                }  
                params =x;
                System.out.println(2);
            }  
            // 建立URL物件   
            java.net.URL connURL = new java.net.URL(url);   
            // 開啟URL連線   
            java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL.openConnection();   
            // 設定通用屬性   
            httpConn.setRequestProperty("Accept", "*/*");   
            httpConn.setRequestProperty("Connection", "Keep-Alive");   
            httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");   
            httpConn.setRequestProperty("Accept-Charset", "UTF-8");  
            httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");  
            // 設定POST方式   
            httpConn.setDoInput(true);   
            httpConn.setDoOutput(true);   
            // 獲取HttpURLConnection物件對應的輸出流   
            out = new PrintWriter(httpConn.getOutputStream());   
            // 傳送請求引數 
            System.out.println("requers parms::"+params);
            out.write(params);   
            // flush輸出流的緩衝   
            out.flush();   
            // 定義BufferedReader輸入流來讀取URL的響應,設定編碼方式   
            in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));   
            String line;   
            // 讀取返回的內容   
            while ((line = in.readLine()) != null) {   
                result += line+"\r\n";   
            }   
        } catch (Exception e) {   
            e.printStackTrace();   
        } finally {   
            try {   
                if (out != null) {   
                    out.close();   
                }   
                if (in != null) {   
                    in.close();   
                }   
            } catch (Exception ex) {   
                ex.printStackTrace();   
            }   
        }   
        return result;   
    }

public static void main(String[] args) {
List<PostValue> p=new ArrayList<PostValue>();
PostValue p1=new PostValue("q_source", "IxhuBltZIjCKM");
p.add(p1);
String sendPost = PostUtils.sendPost("https://dev-openapi.dmhmusic.com/OPENAPI/openApiLogin.json", p);
System.out.println("response result:"+sendPost);
}


}

發現在server端響應的是

通過postMan直接請求發現:


這是一種資料壓縮格式,詳情見:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding

然後將:

 in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));   

修改成:

 GZIPInputStream gzipInputStream = new GZIPInputStream(httpConn.getInputStream());  
           in = new BufferedReader(new InputStreamReader(gzipInputStream));  

最終:


當然也可以使用第三方jar包,比如okHttp.