java 调用 .net web service 问题
服务端地址(115.236.89.174:90/aps.asmx?WSDL)我用jdk自带的axis2和apachecxf等第三方代理工具生成代理类都试过了调用方法pol...
服务端地址(115.236.89.174:90/aps.asmx?WSDL) 我用 jdk自带的 axis2和 apache cxf等第三方代理工具生成代理类都试过了 调用方法poll的时候 没有地方传头部信息设置Security对象 有没有懂webservice的高手帮我看下希望大神能帮我看下 ,下面是我代理生成的 用来调用的 但对象Security 没有办法设置到要请求的方法中
Security s=new Security();
APS ss = new APS();
PollResult objPollResult=new PollResult();
s.setDomainCode("CNLOGINK");
s.setUserID("17750");
s.setUserTokenID("NGQyMmYzMDYtNDRmOS00ZDNlLWFkYWUtMTA3ZDNlYzJkOGFkQVNfUExBVEZPUk1sb2dpbmtfMA");
objPollResult= ss.getAPSSoap().poll(); 求大神们教下 最好能写清楚 感谢
求各位帮忙看下 真心愿意会的加下圈圈1164260967 感谢 这个任务比较急 展开
Security s=new Security();
APS ss = new APS();
PollResult objPollResult=new PollResult();
s.setDomainCode("CNLOGINK");
s.setUserID("17750");
s.setUserTokenID("NGQyMmYzMDYtNDRmOS00ZDNlLWFkYWUtMTA3ZDNlYzJkOGFkQVNfUExBVEZPUk1sb2dpbmtfMA");
objPollResult= ss.getAPSSoap().poll(); 求大神们教下 最好能写清楚 感谢
求各位帮忙看下 真心愿意会的加下圈圈1164260967 感谢 这个任务比较急 展开
3个回答
展开全部
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
public class Servicesend implements Runnable {
private String phone;
private String msg;
private String longnumber;
public Servicesend(String phone, String msg, String longnumber) {
this.phone = phone;
this.msg = msg;
this.longnumber = longnumber;
}
public void run() {
sendPost(
"http://115.236.89.174:90/aps.asmx",
"<soap12:Envelope xmlns:xsi="\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"
+"<soap12:Header><Security xmlns=\"urn:nealnet:query:xsd:1\"><DomainCode>CNLOGINK</DomainCode><UserTokenID>NGQyMmYzMDYtNDRmOS00ZDNlLWFkYWUtMTA3ZDNlYzJkOGFkQVNfUExBVEZPUk1sb2dpbmtfMA
</UserTokenID><UserID>17750</UserID></Security></soap12:Header><soap12:Body>"
+"<Poll xmlns=\"urn:nealnet:query:xsd:1\"><EncryptionType>string</EncryptionType><ServiceTypeID>string</ServiceTypeID><SPsID>string</SPsID><BizQuerys>string</BizQuerys></Poll></soap12:Body></soap12:Envelope>"
);
}//下方4个srting需要自己更改
/**
* javaÄ£Äâpost±íµ¥Ìá½»
*
* @param url
* @param param
* @return
*/
public boolean sendPost(String url, String param) {
boolean bo = true;
StringBuffer buffer = null;
try {
// System.out.println(param);
URL httpurl = new URL(url);
HttpURLConnection httpConn = (HttpURLConnection) httpurl
.openConnection();
httpConn.setRequestProperty("Content-Type", "text/xml;charset=GBK");
httpConn.setRequestProperty("SOAPAction", "\"\"");
httpConn.setRequestMethod("POST");
httpConn.setConnectTimeout(30000);
httpConn.setReadTimeout(30000);
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setUseCaches(false);
PrintWriter out = new PrintWriter(httpConn.getOutputStream());
out.print(param);
out.flush();
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(
httpConn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
buffer = new StringBuffer();
buffer.append(line);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
bo = false;
}
return bo;
}
public static String sendSms(String postXML) throws Exception {
String result="";
String urlString = "";
String soapActionString = "";
URL url = new URL(urlString);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
byte[] buf=postXML.getBytes();
httpConn.setRequestProperty("Content-Length", String.valueOf(buf.length));
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpConn.setRequestProperty("soapActionString", soapActionString);
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
out.write(buf);
out.close();
byte[] datas = readInputStream(httpConn.getInputStream());
result = new String(datas);
return result;
}
public static File replace(String inFileName, String from, String to)
throws IOException, UnsupportedEncodingException {
File inFile = new File(inFileName);
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(inFile), "utf-8"));
File outFile = new File(inFile + ".tmp");
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(outFile), "utf-8")));
String reading;
while ((reading = in.readLine()) != null) {
out.println(reading.replaceAll(from, to));
}
out.close();
in.close();
return outFile;
}
public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return data;
}
public static String sendpost(String mobile){
String xml="";
try {
String param=mobile;
StringBuffer str=new StringBuffer();
str.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
str.append("<soap12:Envelope ");
str.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
str.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"");
str.append(" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">");
str.append(" <soap12:Body> ");
str.append(" <GetUserInfo xmlns=\"http://MonthService.com/\"> ");
str.append(" <key>GZ01SH0062</key>");
str.append(" <phone>"+param+"</phone>");
str.append(" </GetUserInfo>");
str.append(" </soap12:Body>");
str.append(" </soap12:Envelope>");
// System.out.println(str);
xml=sendSms(str.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(xml);
return xml;
}
public static void main(String[] args) {
sendpost("18798669049");
}
}
后面的xml我没有修改,你可以改一下试试postxml过去
你可以参照下这个
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
public class Servicesend implements Runnable {
private String phone;
private String msg;
private String longnumber;
public Servicesend(String phone, String msg, String longnumber) {
this.phone = phone;
this.msg = msg;
this.longnumber = longnumber;
}
public void run() {
sendPost(
"http://115.236.89.174:90/aps.asmx",
"<soap12:Envelope xmlns:xsi="\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"
+"<soap12:Header><Security xmlns=\"urn:nealnet:query:xsd:1\"><DomainCode>CNLOGINK</DomainCode><UserTokenID>NGQyMmYzMDYtNDRmOS00ZDNlLWFkYWUtMTA3ZDNlYzJkOGFkQVNfUExBVEZPUk1sb2dpbmtfMA
</UserTokenID><UserID>17750</UserID></Security></soap12:Header><soap12:Body>"
+"<Poll xmlns=\"urn:nealnet:query:xsd:1\"><EncryptionType>string</EncryptionType><ServiceTypeID>string</ServiceTypeID><SPsID>string</SPsID><BizQuerys>string</BizQuerys></Poll></soap12:Body></soap12:Envelope>"
);
}//下方4个srting需要自己更改
/**
* javaÄ£Äâpost±íµ¥Ìá½»
*
* @param url
* @param param
* @return
*/
public boolean sendPost(String url, String param) {
boolean bo = true;
StringBuffer buffer = null;
try {
// System.out.println(param);
URL httpurl = new URL(url);
HttpURLConnection httpConn = (HttpURLConnection) httpurl
.openConnection();
httpConn.setRequestProperty("Content-Type", "text/xml;charset=GBK");
httpConn.setRequestProperty("SOAPAction", "\"\"");
httpConn.setRequestMethod("POST");
httpConn.setConnectTimeout(30000);
httpConn.setReadTimeout(30000);
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setUseCaches(false);
PrintWriter out = new PrintWriter(httpConn.getOutputStream());
out.print(param);
out.flush();
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(
httpConn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
buffer = new StringBuffer();
buffer.append(line);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
bo = false;
}
return bo;
}
public static String sendSms(String postXML) throws Exception {
String result="";
String urlString = "";
String soapActionString = "";
URL url = new URL(urlString);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
byte[] buf=postXML.getBytes();
httpConn.setRequestProperty("Content-Length", String.valueOf(buf.length));
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpConn.setRequestProperty("soapActionString", soapActionString);
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
out.write(buf);
out.close();
byte[] datas = readInputStream(httpConn.getInputStream());
result = new String(datas);
return result;
}
public static File replace(String inFileName, String from, String to)
throws IOException, UnsupportedEncodingException {
File inFile = new File(inFileName);
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(inFile), "utf-8"));
File outFile = new File(inFile + ".tmp");
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(outFile), "utf-8")));
String reading;
while ((reading = in.readLine()) != null) {
out.println(reading.replaceAll(from, to));
}
out.close();
in.close();
return outFile;
}
public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return data;
}
public static String sendpost(String mobile){
String xml="";
try {
String param=mobile;
StringBuffer str=new StringBuffer();
str.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
str.append("<soap12:Envelope ");
str.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
str.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"");
str.append(" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">");
str.append(" <soap12:Body> ");
str.append(" <GetUserInfo xmlns=\"http://MonthService.com/\"> ");
str.append(" <key>GZ01SH0062</key>");
str.append(" <phone>"+param+"</phone>");
str.append(" </GetUserInfo>");
str.append(" </soap12:Body>");
str.append(" </soap12:Envelope>");
// System.out.println(str);
xml=sendSms(str.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(xml);
return xml;
}
public static void main(String[] args) {
sendpost("18798669049");
}
}
后面的xml我没有修改,你可以改一下试试postxml过去
你可以参照下这个
追问
嗯 谢谢了 这个我知道原生的直接调用评级xml字符串然后http请求 他们叫我用代理工具生成代理类直接调用那样就很多地方不用写死以及得到的结果等等不用直接反序列化成对象 现在问题就是我用第三方生成的代理类的时候不能找不到塞头部sercuity对象的信息
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
.java作为web service 的客户端去调用 .Net Web Service的服务端;
开发中遇到的问题及其解决办法有如下
1)java和.net 的默认namespace问题;
解决方案: java:http://tempuri.org
.net :http://tempuri.org/
区别就是符号“/”;如果不带“/”产生的结果会出错;
问题解决:统一加上“/”;
2)两者产生的SOAP文件格式不一致,有三种,Microsoft的;IBM的,通过抓包工具可以获得java产生的Soap消息
解决方案:SOAP 消息头问题,从上面抓包的信息可以看出:请求的是<soapenv:Envelope 而response的是<soap:Envelope
这是由于不同的服务商封装成的不同格式的soap ,但都是遵循SOAP,可以通用;
Microsoft的是 <soap:Envelope ,而IBM的是 <soapenv:Envelope ;
3)入参的数据类型会影响调用的正确性;
解决方案:
第二个参数的入参是long型。如果修改make1方法的String 型成 long 型,则程序异常;
AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Client
faultSubcode:
faultString: System.Web.Services.Protocols.SoapException: Server was unable to read request. ---> System.InvalidOperationException: There is an error in XML document (1, 440). ---> System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt64(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Xml.XmlConvert.ToInt64(String s)
其实我们只需要在参数设置里面设成org.apache.axis.encoding.XMLType.XSD_LONG;
开发中遇到的问题及其解决办法有如下
1)java和.net 的默认namespace问题;
解决方案: java:http://tempuri.org
.net :http://tempuri.org/
区别就是符号“/”;如果不带“/”产生的结果会出错;
问题解决:统一加上“/”;
2)两者产生的SOAP文件格式不一致,有三种,Microsoft的;IBM的,通过抓包工具可以获得java产生的Soap消息
解决方案:SOAP 消息头问题,从上面抓包的信息可以看出:请求的是<soapenv:Envelope 而response的是<soap:Envelope
这是由于不同的服务商封装成的不同格式的soap ,但都是遵循SOAP,可以通用;
Microsoft的是 <soap:Envelope ,而IBM的是 <soapenv:Envelope ;
3)入参的数据类型会影响调用的正确性;
解决方案:
第二个参数的入参是long型。如果修改make1方法的String 型成 long 型,则程序异常;
AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Client
faultSubcode:
faultString: System.Web.Services.Protocols.SoapException: Server was unable to read request. ---> System.InvalidOperationException: There is an error in XML document (1, 440). ---> System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt64(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Xml.XmlConvert.ToInt64(String s)
其实我们只需要在参数设置里面设成org.apache.axis.encoding.XMLType.XSD_LONG;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你看看你的防火墙关闭了没。
追问
跟防火墙没关系吧 我用代理工具生成的代理类好像有问题 没有办法设置请求头部 对象Security 信息是用户信息要设置到请求的方法里面
追答
我这有几个用hesssion和xfire做的webservice的测试例子,能运行, 不知道你用的着不,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询