java实现之测试第三方接口是否能正确连接
来源:互联网 发布:门诊流程优化 编辑:程序博客网 时间:2024/05/16 08:24
package com.hz.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) {
if(true){
String aa=null;
System.out.println(aa);
return ;
}
String address = "http://spm01.gdpr.com/Webservice/NPMService1.asmx?wsdl";
testWsdlConnection(address);
}
public static int testWsdlConnection(String address) {
String WSDL = "http://127.0.0.1:8080/SMSService/webservices/SMSService";
String result = null;
try {
result = callWebService(WSDL);
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println(result);
int status = 404;
try {
URL urlObj = new URL(address);
HttpURLConnection oc = (HttpURLConnection) urlObj.openConnection();
oc.setUseCaches(false);
oc.setConnectTimeout(3000); // 设置超时时间
status = oc.getResponseCode();// 请求状态
if (200 == status) {
System.out.println("请求地址顺利连通。。GjcbdwxxUtil。" + address);
return status;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("请求地址不通。。GjcbdwxxUtil。" + address);
}
return status;
}
private static String actionBySOAP() {
StringBuilder sb = new StringBuilder();
sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas"
+ ".xmlsoap.org/soap/envelope/\" xmlns:sms=\"helloworld\">");
sb.append("<soapenv:Header/>");
sb.append("<soapenv:Body>");
sb.append("<sms:sendSMS>");
sb.append("<sms:in0>123</sms:in0>");
sb.append("</sms:sendSMS>");
sb.append("</soapenv:Body>");
sb.append("</soapenv:Envelope>");
return sb.toString();
}
private static String callWebService(String wsdl) throws Exception {
System.setProperty("sun.net.client.defaultConnectTimeout", "20000");
System.setProperty("sun.net.client.defaultReadTimeout", "20000");
// URL连接
URL url = new URL(wsdl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// conn.setRequestMethod("GET");
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Length",
String.valueOf(actionBySOAP().getBytes().length));
conn.setRequestProperty("Content-Type", "text/xml; charset=GBK");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(20000);
// 请求输入内容
OutputStream output = conn.getOutputStream();
output.write(actionBySOAP().getBytes());
output.flush();
output.close();
// 请求返回内容
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String str = null;
while ((str = br.readLine()) != null) {
sb.append(str + "\n");
}
br.close();
isr.close();
return sb.toString();
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) {
if(true){
String aa=null;
System.out.println(aa);
return ;
}
String address = "http://spm01.gdpr.com/Webservice/NPMService1.asmx?wsdl";
testWsdlConnection(address);
}
public static int testWsdlConnection(String address) {
String WSDL = "http://127.0.0.1:8080/SMSService/webservices/SMSService";
String result = null;
try {
result = callWebService(WSDL);
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println(result);
int status = 404;
try {
URL urlObj = new URL(address);
HttpURLConnection oc = (HttpURLConnection) urlObj.openConnection();
oc.setUseCaches(false);
oc.setConnectTimeout(3000); // 设置超时时间
status = oc.getResponseCode();// 请求状态
if (200 == status) {
System.out.println("请求地址顺利连通。。GjcbdwxxUtil。" + address);
return status;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("请求地址不通。。GjcbdwxxUtil。" + address);
}
return status;
}
private static String actionBySOAP() {
StringBuilder sb = new StringBuilder();
sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas"
+ ".xmlsoap.org/soap/envelope/\" xmlns:sms=\"helloworld\">");
sb.append("<soapenv:Header/>");
sb.append("<soapenv:Body>");
sb.append("<sms:sendSMS>");
sb.append("<sms:in0>123</sms:in0>");
sb.append("</sms:sendSMS>");
sb.append("</soapenv:Body>");
sb.append("</soapenv:Envelope>");
return sb.toString();
}
private static String callWebService(String wsdl) throws Exception {
System.setProperty("sun.net.client.defaultConnectTimeout", "20000");
System.setProperty("sun.net.client.defaultReadTimeout", "20000");
// URL连接
URL url = new URL(wsdl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// conn.setRequestMethod("GET");
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Length",
String.valueOf(actionBySOAP().getBytes().length));
conn.setRequestProperty("Content-Type", "text/xml; charset=GBK");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(20000);
// 请求输入内容
OutputStream output = conn.getOutputStream();
output.write(actionBySOAP().getBytes());
output.flush();
output.close();
// 请求返回内容
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String str = null;
while ((str = br.readLine()) != null) {
sb.append(str + "\n");
}
br.close();
isr.close();
return sb.toString();
}
}
0 0
- java实现之测试第三方接口是否能正确连接
- java请求第三方接口
- 测试数据库是否连接正确的实例
- 利用第三方接口,实现地区联动
- python 接口实现 供第三方调用
- java实现第三方登录
- Java Web 常用的第三方接口
- java调用第三方支付接口
- java开发 第三方支付 接口
- java写第三方支付接口
- java第三方支付接口(云通付)
- 测试接口是否正确应考虑的因素
- httpcilent连接第三方接口请求(post,get)
- 第三方QQ接口的登陆基础测试
- 局域网使用NAT进行测试第三方接口
- 第三方常见接口
- webservice 第三方接口
- 第三方接口
- 【机器学习】SVM学习(四):为何需要核函数
- 通过反射获取当前Activity实例
- 旧手机这桩生意 爱回收、转转和闲鱼谁做的更好?
- Unity中的优化技术
- 记录学习的路程
- java实现之测试第三方接口是否能正确连接
- 生成博客
- 正走向大牛的菜鸟
- 取一个整数a从右端开始的4~7位
- UVALive 6672 Bonus Cards
- 开机出现 system32\config\system,代码:0xc00000e9解决方法
- 浅谈super与this关键字的区别
- 几个 学习网站
- ElasticSearch 常见错误