JAVA Http协议的发送接收

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.*;
import javax.servlet.http.*;

public class CommServer extends HttpServlet {

 private static final String CONTENT_TYPE = "text/html; charset=GBK";           //编码类型

 public void init() throws ServletException{                                    //初始化函数
 }

 public void yReceiverSend(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
  
  response.setContentType(CONTENT_TYPE);
  String ip = request.getRemoteHost();                                   //IP地址
  String url = request.getRequestURI();                                   //url地址
  InputStream inputStream = request.getInputStream();                       //用二进制流接收数据
  BufferedInputStream input = null;                                       //输入流,用于接收请求的数据
        byte[] buffer = new byte[1024];                                             //数据缓冲区
        int count = 0;                                                            //每个缓冲区的实际数据长度
        ByteArrayOutputStream streamXML = new ByteArrayOutputStream();            //请求数据存放对象
        byte[] iXMLData = null;                                                     //请求数据存放到byte[]

  //接收银行数据
  try {
   input = new BufferedInputStream(inputStream);
   while ((count = input.read(buffer)) != -1){
    streamXML.write(buffer, 0, count);
   } 
  }catch (Exception e){
   e.printStackTrace();
  }finally{
   if (input != null){
    try {
     input.close();
    }catch (Exception f){
     f.printStackTrace();
    }
   }
  }
  iXMLData = streamXML.toByteArray();                                              //得到一个byte数组,提供给平台
/
//调用相应平台程序的过程.......
//                     ......
//                     ......

  //返回数据给银行
  String returnStr = null;                                                        //平台程序返回的数据

  try {
   OutputStream out = response.getOutputStream();
   response.setContentLength(returnStr.getBytes().length);                      //添加响应包长度
   out.write(returnStr.getBytes());
   out.flush();
   out.close();
  } catch (Exception e){
   e.printStackTrace();
  }
 }
 
 
 public void bSendReceiver() throws Exception{                                      //平台主动发起的http发送
  
  try{
   String url = "http://www.baidu.com";                                         //查表得到发送地址

   HttpURLConnection httpConnection = (HttpURLConnection) new URL(url)         //建立一个HttpURLConnection
     .openConnection();

   httpConnection.setRequestMethod("POST");
   httpConnection.setDoOutput(true);
   httpConnection.setDoInput(true);
   httpConnection.setAllowUserInteraction(true);
   httpConnection.connect();

   //发送数据给保险公司
   OutputStream outputStream = httpConnection.getOutputStream();
   byte[] buffer = new byte[1024];                                            //平台生成的XML串
            outputStream.write(buffer);
            outputStream.flush();
            outputStream.close();

            //接收保险公司的数据数据
            InputStream inputStream = httpConnection.getInputStream();
      BufferedInputStream input = null;                                       //输入流,用于接收请求的数据
            buffer = new byte[1024];                                                    //数据缓冲区
            int count = 0;                                                            //每个缓冲区的实际数据长度
            ByteArrayOutputStream streamXML = new ByteArrayOutputStream();            //请求数据存放对象
            byte[] iXMLData = null;                                                     //请求数据存放到byte[]

      try {
       input = new BufferedInputStream(inputStream);
       while ((count = input.read(buffer)) != -1){
        streamXML.write(buffer, 0, count);
       } 
      }catch (Exception e){
       e.printStackTrace();
      }finally{
       if (input != null){
        try {
         input.close();
        }catch (Exception f){
         f.printStackTrace();
        }
       }
      }
      iXMLData = streamXML.toByteArray();                                           //得到一个byte数组,提供给平台
   httpConnection.disconnect();
  }catch (Exception e){
   e.printStackTrace();
  }
 }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值