Java 請求webServce接口 帶參數

public String  getWebServiceByParams(String param){   //獲取基金繳付記錄
        // Post請求的url,與get不一樣的是不須要帶參數
        URL postUrl = null;
        try {
            postUrl = new URL(""); //***這裏寫webService接口地址
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 打開鏈接
        HttpURLConnection connection = null;
        try {
            connection = (HttpURLConnection) postUrl.openConnection();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }     
        // 設置是否向connection輸出,由於這個是post請求,參數要放在
        // http正文內,所以須要設爲true
        connection.setDoOutput(true);
        // Read from the connection. Default is true.
        connection.setDoInput(true);
        // 默認是 GET方式
        try {
            connection.setRequestMethod("POST");
        } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }      
        // Post 請求不能使用緩存
        connection.setUseCaches(false);  
        //設置本次鏈接是否自動重定向 
        connection.setInstanceFollowRedirects(true);      
        // 配置本次鏈接的Content-type,配置爲application/x-www-form-urlencoded的
        // 意思是正文是urlencoded編碼過的form參數
        connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        // 鏈接,從postUrl.openConnection()至此的配置必需要在connect以前完成,
        // 要注意的是connection.getOutputStream會隱含的進行connect。
        try {
            connection.connect();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        DataOutputStream out = null;
        try {
            out = new DataOutputStream(connection
                    .getOutputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 正文,正文內容其實跟get的URL中 '? '後的參數字符串一致
        String content = null;
        try {
            content = "createDate=" + URLEncoder.encode(param, "UTF-8");   //這裏改爲你的參數名字
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // DataOutputStream.writeBytes將字符串中的16位的unicode字符以8位的字符形式寫到流裏面
        try {
            out.writeBytes(content);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        //流用完記得關
        try {
            out.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //獲取響應
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        String line = null;
        //        while ((line = reader.readLine()) != null){
        //            System.out.println("line---?"+line);
        //            
        //        }
        try {
            line = reader.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("line---?"+line);

        try {
            reader.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //該乾的都幹完了,記得把鏈接斷了
        connection.disconnect();
        return line;
    }
相關文章
相關標籤/搜索