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();
    }
    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值