HTTP+XML方式接口数据传输

一.客户端

1.请求URL;如http://10.46.34.233/fcgi-bin/UIG_SFC_186

2.发送请求数据

{

   组装报文格式发送信息

   StringBuffer sendStr = new StringBuffer(1024);
   sendStr.append("<?xml version=/"1.0/" encoding=/"GBK/"?>");
   sendStr.append("<operation_in type=/"struct/">");
   sendStr.append("<service_name type=/"string/">RDMP_ToDoTaskQuery</service_name>");
   sendStr.append("<sysfunc_id type=/"int/">61590334</sysfunc_id>");
   sendStr.append("<request_type type=/"int/">1002</request_type>");
   sendStr.append("<verify_code type=/"string/">789730661774598915448481</verify_code>");
   sendStr.append("<operator_id type=/"string/">"+operator_id+"</operator_id>");
   sendStr.append("<organ_id type=/"int/">0000000</organ_id>");
   sendStr.append("<request_time type=/"datetime/">"+System.currentTimeMillis()/1000+"</request_time>");
   sendStr.append("<request_seq type=/"long/"></request_seq>");
   sendStr.append("<request_source type=/"int/">304061</request_source>");
   sendStr.append("<request_target type=/"int/">201024</request_target>");
   sendStr.append("<msg_version type=/"string/">0100</msg_version>");
   sendStr.append("<cont_version type=/"string/">0100</cont_version>");
   sendStr.append("<content type=/"string/">");
   sendStr.append("<![CDATA[");
   sendStr.append("<?xml version=/"1.0/" encoding=/"GBK/"?>");
   sendStr.append("<request type=/"struct/">");
   sendStr.append("<operator_id>"+operator_id+"</operator_id>");
   sendStr.append("</request>");
   sendStr.append("]]>");
   sendStr.append("</content>");
   sendStr.append("</operation_in>");
  

 

   发送报文格式发送信息

   URL url = null;
   HttpURLConnection conn = null;
   try{

    //HTTP的请求URL

    url = new URL(http://10.46.34.233/fcgi-bin/UIG_SFC_186);
    conn = (HttpURLConnection)url.openConnection();
    conn.setRequestProperty("Pragma:", "no-cache");
    conn.setRequestProperty("Content-Type", "text/xml;charset=GBK");
    //HTTP的POST方式进行数据传输
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    OutputStream dataOut = conn.getOutputStream();
    StringBuffer sendStr = new StringBuffer(1024);
    dataOut.write(content.getBytes());
    dataOut.write(sendStr.toString().getBytes());
    dataOut.flush();
    dataOut.close();
    //获取响应内容
   try{
     conn.connect();

     BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
     //注意BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"GBK")); 这么写,是因为有时候网络传输过程中字符会被修改,如GB-2312,因为出现乱码,就需要将此处加入GBK。
    //loger.info("获取默认字符编码:"+Charset.defaultCharset());
    String line = "";
    StringBuffer buffer = new StringBuffer(1024);
    while((line=in.readLine())!=null){
     buffer.append(line);
    }
    in.close();
    result = buffer.toString();
     //获取到的回馈信息:+result+结果集。
    }catch(IOException e){
     e.printStackTrace();
    }catch(Exception e){
     e.printStackTrace();
    }
   }catch(Exception e){
    e.printStackTrace();
   }

}

二.服务器端

1.提供访问URL;如http://10.11.26.101/ce_boa/Request_SERVET

2.获取监听到的内容

{

  BufferedReader inputStream = new BufferedReader(new InputStreamReader(request.getInputStream()));
  StringBuffer buffer = new StringBuffer(1024);
  String line = "";
  String isSuccessed;
  while((line=inputStream.readLine()) != null)
  {
   buffer.append(line);
  }
  String result = buffer.toString();
  loger.info("获取监听到的内容:"+result);
  WorkProcessContent workContent = new WorkProcessContent();
  //监听内容
  if(!result.equals(""))
  {

   //进行业务处理

   StringReader reads = new StringReader(result.toString());
     InputSource sources = new InputSource(reads);
     SAXBuilder sax = new SAXBuilder();
     try {
      Document doc = sax.build(sources);
      Element root =doc.getRootElement();
      String content = root.getChildText("content");//得到数据TEST
      StringReader readesh= new StringReader(content.toString());
         InputSource sourcere= new InputSource(readesh);
         SAXBuilder resb = new SAXBuilder();
         Document redoc = resb.build(sourcere);
         //得到根节点
         Element taskinfos = redoc.getRootElement();
         List listAttachInfo = taskinfos.getChildren();
         for (int i = 0; i < listAttachInfo.size(); i++)
        {

             Element taskinfo = (Element) listAttachInfo.get(i);
             String task_id = taskinfo.getChildText("task_id");
             .........

     }

   }

}

 

阅读更多
个人分类: 接口
想对作者说点什么? 我来说一句

HTTPXML 测试工具

2017年12月12日 5KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭