欢迎使用普元产品知识库,本知识库包含普元应用开发平台EOSPlatform,流程平台BPS,企业服务总线ESB,微服务平台Microservice,运维管理平台Devops,数据集成平台DI
欢迎使用普元文档库
【问题描述】
在使用java方法调用经过ESB穿透过的服务时的调用方法,以及加header时的demo
【解答】
TestClient.java为java方式调用的客户端类,以及增加这两个头信息,请查收进行参考,根据自己环境进行调试和修改
package com.primeton.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestClient {
public static void main(String[] args) throws FileNotFoundException, IOException {
//请求的URL地址
// String urlString = "http://127.0.0.1:9090/queryService";
String urlString = "https://127.0.0.1:8443/examples/servlets/servlet/HelloWorldExample";
//请求的报文内容,传入的类型为soap类型时,报文写到"sendsoap.xml"中,json时写到"json.txt"中
// String xmlFile = "c.xml";
//soap请求类型时的soap operation
String soapActionString = "";
URL url = new URL(urlString);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
// File fileToSend=new File(xmlFile);
// byte[] buf=new byte[(int)fileToSend.length()];
// new FileInputStream(xmlFile).read(buf);
// httpConn.setRequestProperty("Content-Length",String.valueOf( buf.length ) );
httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
//请求需要的一些消息头
httpConn.setRequestProperty("ClientId","");
httpConn.setRequestProperty("OperationCode","");
// httpConn.setRequestProperty("SOAPAction",soapActionString);
// httpConn.setRequestMethod( "POST" );
httpConn.setRequestMethod( "GET" );
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
// httpConn.setReadTimeout(3000);
OutputStream out = httpConn.getOutputStream();
// out.write( buf );
out.close();
InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(),"utf-8");
BufferedReader in = new BufferedReader(isr);
String inputLine;
StringBuffer sb =new StringBuffer();
while ((inputLine = in.readLine()) != null)
in.close();
System.out.println(sb.toString());
}
}