HttpURLConnection 实现POST提交

package com.mmt.test.urlConnection;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;


public class URLConnection_POST {
    
    
    public static void main(String []args){
        String urlPath ="http://localhost/test/userLogin.php";
        String encodeType ="utf-8";
        
        Map<Object,Object> params = new HashMap<Object,Object>();
        params.put("userName", "mmt");
        params.put("userPswd","123456");
        String resultString = doPost(urlPath,params,encodeType);
        System.out.println(resultString);
    }
    
    public static String doPost(String urlPath,Map<Object,Object>params,String encodeType){
        InputStream  inputStream = doPostAndGetStream(urlPath,params);
        String resultString = streamToString(inputStream, encodeType);
        return resultString ;
    }
    
    
    private static InputStream doPostAndGetStream(String urlPath,Map<Object,Object>params){
        
        InputStream inputStream =  null ;
        java.io.OutputStream outputStream = null ;
        URL url = null ;
        try {
            url = new URL(urlPath);
            HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
            httpURLConnection.setConnectTimeout(3000);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            
            httpURLConnection.setRequestMethod("POST");
            StringBuffer stringBuffer = new StringBuffer();
            for(Entry<Object,Object> entry:params.entrySet()){
                stringBuffer.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
            stringBuffer.deleteCharAt(stringBuffer.length()-1);
            
            outputStream = httpURLConnection.getOutputStream();
            outputStream.write(stringBuffer.toString().getBytes());
            
            inputStream = httpURLConnection.getInputStream();
            
        
            
            
            
            
        } catch (Exception e) {
            // TODO: handle exception
        }
        
        return inputStream ;
        
    }
    
    private static String streamToString(InputStream inputStream,String encodeType){
        String resultString = null ;
        
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int len = 0;
        byte data[]=new byte[1024];
        try {
            while((len=inputStream.read(data))!=-1){
                byteArrayOutputStream.write(data,0,len);
            }
            byte[]allData = byteArrayOutputStream.toByteArray();
            resultString = new String(allData,encodeType);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        return resultString ;
    }
    
}
View Code

 

posted @ 2018-12-01 23:55  梦想战士  阅读(3006)  评论(0编辑  收藏  举报