java 遠程訪問接口

package com.test;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringBufferInputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class TestStateQuery {
    
    
    public String connectServlet(String url,String businessnoXml){
        String strReturnXML="";
        HttpURLConnection httpConnection;
        try {
            // 1、打開連接                        
            httpConnection = (HttpURLConnection) new URL(url).openConnection();
            httpConnection.setRequestMethod("POST");
            httpConnection.setDoOutput(true);
            httpConnection.setDoInput(true);
            httpConnection.setAllowUserInteraction(true);
            httpConnection.connect();                        
            // 2、發送數據                
            OutputStream outputStream = httpConnection.getOutputStream();
            
            
            byte[] buffer = new byte[1024];                        
            // InputStream stringBufferInputStream = new StringBufferInputStream(businessnoXml);    
            
            InputStream stringBufferInputStream =  new ByteArrayInputStream(businessnoXml.getBytes());
            
            while (true) {
                int bytesRead = stringBufferInputStream.read(buffer);
                if (bytesRead == -1)
                    break;
                 outputStream.write(buffer, 0, bytesRead);
                //outputStream.write(buffer);
            }
            
            outputStream.flush();
            stringBufferInputStream.close();
            outputStream.close();
            // 3、返回數據
            InputStreamReader inputStreamReader = new InputStreamReader(httpConnection.getInputStream());                        
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);                
            String inputLine = "";
            StringBuffer inputLines = new StringBuffer();                        
            while ((inputLine = bufferedReader.readLine()) != null) {
                inputLines.append(inputLine);
            }
            inputStreamReader.close();
            bufferedReader.close();
            // 4、關閉連接
            httpConnection.disconnect();                        
            strReturnXML = inputLines.toString();
            
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
        
        return strReturnXML.toString();
    }

    
    public static void main(String[] args) {
        String resultXML = "";
        TestStateQuery test = new TestStateQuery();
        String url = "http://localhost:7001/undwrt/ServletName";
        String  filePath ="E:/sendXML/zhongxin.xml";
        resultXML = test.connectServletUserXmlFile(url, filePath);
        //resultXML = test.connectServlet( url, businessno);
        System.out.println("resultXML:"+resultXML);
    }
    
    
    public String connectServletUserXmlFile(String url,String filePath){
        String strReturnXML="";
        HttpURLConnection httpConnection;
        try {
            // 1、打開連接                        
            httpConnection = (HttpURLConnection) new URL(url).openConnection();
            httpConnection.setRequestMethod("POST");
            httpConnection.setDoOutput(true);
            httpConnection.setDoInput(true);
            httpConnection.setAllowUserInteraction(true);
            httpConnection.connect();                        
            // 2、發送數據                
            OutputStream outputStream = httpConnection.getOutputStream();
            byte[] buffer = new byte[1024];                        
            // InputStream stringBufferInputStream = new StringBufferInputStream(businessnoXml);    
            
            //InputStream stringBufferInputStream =  new ByteArrayInputStream(businessnoXml.getBytes());
            File file = new File(filePath);
            
            InputStream in = new FileInputStream(file);
            
            while (true) {
                int bytesRead = in.read(buffer);
                if (bytesRead == -1)
                    break;
                 outputStream.write(buffer, 0, bytesRead);
                //outputStream.write(buffer);
            }
            
            outputStream.flush();
            in.close();
            outputStream.close();
            // 3、返回數據
            InputStreamReader inputStreamReader = new InputStreamReader(httpConnection.getInputStream());                        
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);                
            String inputLine = "";
            StringBuffer inputLines = new StringBuffer();                        
            while ((inputLine = bufferedReader.readLine()) != null) {
                inputLines.append(inputLine);
            }
            inputStreamReader.close();
            bufferedReader.close();
            // 4、關閉連接
            httpConnection.disconnect();                        
            strReturnXML = inputLines.toString();
            
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
        
        return strReturnXML.toString();
    }
    

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章