Webserviceテスト
9967 ワード
package com.test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class SendData extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel();
JLabel label0 = new JLabel("URL:");
JLabel label1 = new JLabel("Request Info:");
JLabel label2 = new JLabel("Result:");
JTextField urlField = new JTextField();
JTextArea sendArea = new JTextArea();
JTextArea resultArea = new JTextArea();
JButton button1 = new JButton("Execute");
JButton button2 = new JButton("Exit");
public SendData() {
panel.setLayout(null);
label0.setBounds(10, 10, 80, 20);
label1.setBounds(10, 40, 80, 50);
label2.setBounds(10, 250, 80, 20);
urlField.setBounds(110, 10, 800, 25);
sendArea.setBounds(110, 40, 800, 200);
// sendArea.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
resultArea.setBounds(110, 250, 800, 200);
// resultArea.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
sendArea.setWrapStyleWord(true);//
sendArea.setLineWrap(true);//
resultArea.setWrapStyleWord(true);
resultArea.setLineWrap(true);
button1.setBounds(300, 500, 80, 30);
button2.setBounds(500, 500, 80, 30);
JScrollPane scroll = new JScrollPane(sendArea);
scroll.setBounds(110, 40, 800, 200);
// scroll.setHorizontalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
JScrollPane scroll2 = new JScrollPane(resultArea);
scroll2.setBounds(110, 250, 800, 200);
// scroll2.setHorizontalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
panel.add(scroll);
panel.add(scroll2);
panel.add(label0);
panel.add(label1);
panel.add(label2);
panel.add(urlField);
//http://nkweb-uat.huawei.com/tpwms/services/queryByImei
urlField.setText("http://nkweb-uat.huawei.com/tpwms/services/queryByImei");
// panel.add(sendArea);
// panel.add(resultArea);
panel.add(button1);
panel.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
add(panel, BorderLayout.CENTER);
this.setTitle("webservice test tool");
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
if (sendArea.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "Plase input send Data", "Message", JOptionPane.ERROR_MESSAGE);
return;
}
String result = null;
try {
result = doSendData(urlField.getText(), sendArea.getText());
} catch (IOException e1) {
e1.printStackTrace();
}
resultArea.setText(result);
}
if (e.getSource() == button2) {
System.exit(0);
}
}
public static void main(String args[]) {
System.setProperty("file.encoding","utf-8");
SendData login = new SendData();
login.setDefaultCloseOperation(SendData.EXIT_ON_CLOSE);
login.setSize(1000, 600);
login.setVisible(true);
}
/**
* ,
* @throws IOException
*/
private static String doSendData(String url, String sendData) throws IOException {
HttpURLConnection conn = null;
OutputStream outPutStream = null;
Writer writer = null;
BufferedReader br = null;
try {
//
conn = getURLConnection(url, "POST");
conn.setRequestProperty("Content-Type", "text/xml");
conn.connect();
outPutStream = conn.getOutputStream();
writer = new OutputStreamWriter(outPutStream, "UTF-8");
//
writer.write(sendData);
writer.flush();
br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
// URL
StringBuilder sb = getMessageFromReader(br);
String str = sb.toString();
//System.out.println("result: " + str);
//
Document doc = DocumentHelper.parseText(str);
OutputFormat format = new OutputFormat(" ", true, "UTF-8");
format.setTrimText(true);
format.setLineSeparator("
");
XMLWriter output = new XMLWriter(new FileWriter(new File("response\\response.xml")), format);
output.write(doc);
output.close();
//
SAXReader reader = new SAXReader();
Document document = reader.read(new File("response\\response.xml"));
String result= document.asXML();
System.out.println("result: " + result);
System.out.println("--------------Response Code:"+ conn.getResponseCode());
return result;
} catch (Exception e) {
System.out.println("--------------Response Code:"+ conn.getResponseCode());
e.printStackTrace();
}
finally{
}
return null;
}
/**
* HTTP
*
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
private static HttpURLConnection getURLConnection(String serviceUrl, String restMethod)
throws MalformedURLException, IOException, ProtocolException, NoSuchAlgorithmException,
KeyManagementException {
String http = serviceUrl.split(":")[0];
if (http.equalsIgnoreCase("http")) {
URL url = new URL(serviceUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(restMethod);
conn.setReadTimeout(100000);
conn.setConnectTimeout(100000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
return conn;
} else if (http.equalsIgnoreCase("https")) {
URL url = new URL(serviceUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod(restMethod);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setReadTimeout(100000);
conn.setConnectTimeout(100000);
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { new iTrustManager() }, new java.security.SecureRandom());
conn.setSSLSocketFactory(sc.getSocketFactory());
return conn;
}
return null;
}
static class iTrustManager implements X509TrustManager {
iTrustManager() {
}
// check client trust status
public void checkClientTrusted(X509Certificate chain[], String authType) throws CertificateException {
System.out.println("check client trust status");
}
// check Server trust status
public void checkServerTrusted(X509Certificate chain[], String authType) throws CertificateException {
System.out.println("check Server trust status");
}
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
String paramString) throws java.security.cert.CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
String paramString) throws java.security.cert.CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
}
/**
*
*/
private static StringBuilder getMessageFromReader(BufferedReader br) throws IOException {
StringBuilder sb = new StringBuilder();
String returnStrItem = null;
while ((returnStrItem = br.readLine()) != null) {
sb.append(returnStrItem);
}
return sb;
}
}