java通过中间代理访问webservice接口,请大佬帮忙看下。
public String getService(String methodName, String xml) {
try {
String endpointURL = "http://*,*,*,*:8086/ccpay/services/PayService?wsdl";
AxisProperties.setProperty("http.proxyHost", ip);//代理IP
AxisProperties.setProperty("http.proxyPort", "8080");//代理端口
AxisProperties.setProperty("http.proxyUser", "43310107"); //用户名
AxisProperties.setProperty("http.proxyPassword", "123456"); //密码
//有个疑问,这里设置了property,但是我看后面的代码并没有用到这些呀,这个是怎么生效的?
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpointURL);
call.addParameter("test", XMLType.XSD_STRING, ParameterMode.IN);
call.setOperationName(methodName);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
String ret = (String) call.invoke(new Object[] { new String(xml) });
return ret;
}
catch (Exception e)
{
return "";
}
}
调用方法为:
getService("getcustwaterrate",message);
这里的message我送的是一串json报文
单步执行到这一句会报空指针错误:
String ret = (String) call.invoke(new Object[] { new String(xml) });
但是我看这里面的xml就是我送的json,不应该报空指针,百思不得其解。
如果asix代理方式不能用
那如下这种proxy代理方式可用不?我这个电脑是没环境,我明天再试一下。
URL url = new URL("http://www.shanhe114.com");
//创建代理服务器
InetSocketAddress addr = new InetSocketAddress("10.28.0.4",
8080);
//Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); //SOCKS代理
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); //HTTP代理
//其他方式可以见Proxy.Type属性
URLConnection conn = url.openConnection(proxy);
InputStream in = conn.getInputStream();
//InputStream in = url.openStream();
String content = IOUtils.toString(in);
System.out.println(content);
以下是验密的方法和类。
public class MyAuthenticator extends Authenticator {
private String username = "";
private String password = "";
public MyAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
returnnew PasswordAuthentication(username, password.toCharArray());
}
}
//设置登陆到代理服务器的用户名和密码
Authenticator.setDefault(new MyAuthenticator("userName", "Password"));