可以用来测试 PO SOAP/REST
//import org.dom4j.*;
//import org.dom4j.io.SAXReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
//import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
//import java.net.URLEncoder;
//import java.sql.Timestamp;
import java.util.Base64;
//import java.util.Date;
/***
* Call WebServices by HTTP Request
*
* @author wy
*
*/
public class httpWebReq {
public static String urlEncode(String source, String encode) {
String result = source;
try {
result = java.net.URLEncoder.encode(source, encode);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
return result;
}
/***
* Call WebServices by Http post
*
* @param uri - WSDL Endpoint
* @param data - Message Content
* @param auth - Authority
*/
public static String CallWShttp(String uri, String data, String auth, String content) {
StringBuffer buffer = new StringBuffer();
OutputStream output = null;
InputStream input = null;
HttpURLConnection urlConn = null;
try {
URL url = new URL(uri);
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestMethod("POST");
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-Type", content);
urlConn.setReadTimeout(9000);
urlConn.setConnectTimeout(20000);
urlConn.setRequestProperty("Authorization", "Basic " + auth);
//keytool -importcert -noprompt -trustcacerts -alias dev //导入证书>不提示>信任来自cacerts的证书
//-file "c:\dev.cer" -keystore "C:\Program Files\Java\jdk1.8.0_131\jre\lib\security\cacerts" //证书文件>密钥库名称
//-storepass changeit //密钥库口令
urlConn.connect();
// post data
output = urlConn.getOutputStream();
output.write(data.getBytes("UTF-8"));
output.flush();
System.out.println(urlConn.getResponseCode());
if (urlConn.getResponseCode() < 400) {
input = urlConn.getInputStream();
} else {
input = urlConn.getErrorStream();
}
InputStreamReader isr = new InputStreamReader(input, "utf-8");
BufferedReader br = new BufferedReader(isr);
String str = null;
while ((str = br.readLine()) != null) {
buffer.append(str);
}
br.close();
isr.close();
input.close();
output.close();
urlConn.disconnect();
} catch (Exception e) {
System.out.println("Error:" + e.getMessage());
e.printStackTrace();
}
return buffer.toString();
}
public static String readText(String fileName,String encode) {
String fileContent = "";
try {
File f = new File(fileName);
if (f.isFile() && f.exists()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(f), encode);
BufferedReader reader = new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
fileContent += line;
}
read.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return fileContent;
}
public static void ConvEncodeing(String fileName,String outName , String encode) {
try {
File fi = new File(fileName);
File fo = new File(outName);
OutputStream os = new FileOutputStream(fo);
if (fi.isFile() && fi.exists()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(fi), encode);
BufferedReader reader = new BufferedReader(read);
int line = 0;
while ((line = reader.read()) > 0) {
os.write(line);;
}
read.close();
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
String i_content = "";
String i_auth = "";
String i_contype = System.getProperty("contype");
String i_url = System.getProperty("url");
String i_path = System.getProperty("path");
String i_user = System.getProperty("user");
String i_pwd = System.getProperty("pwd");
// Endpoint
if(i_url.isEmpty()){
i_url = "https://<host>:<port>/RESTAdapter/demo";
}
if(i_path.isEmpty()){
java.io.File directory = new java.io.File(".");
i_path = directory.getCanonicalPath() + "\\src\\demo.xml";
}
if(i_contype.isEmpty()){
i_contype = "application/json;charset=utf-8";
}
i_content = httpWebReq.readText(i_path,"utf-8");
if(i_user.isEmpty()){
i_auth = "********************";
}else{
Base64.Encoder encoder = Base64.getEncoder();
i_auth = new String(encoder.encode(( i_user + ":" + i_pwd ).getBytes()));
}
//System.out.println(new Timestamp(new Date().getTime()));
System.out.println(httpWebReq.CallWShttp(i_url, i_content, i_auth, i_contype));
//System.out.println(new Timestamp(new Date().getTime()));
}
}