加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何直接soap字符串,访问webservice

发布时间:2020-12-17 01:07:53 所属栏目:安全 来源:网络整理
导读:1.Webservice.GetVcardByUserNo(String userId,String userNo);这个是封装了的webservice接口。 2.在程序中连续两次调用该接口时,ksoap2在解析第二次调用返回的结果时抛异常。 ? ? 异常信息如下:org.xmlpull.v1.XmlPullParserException: unexpected type

1.Webservice.GetVcardByUserNo(String userId,String userNo);这个是封装了的webservice接口。
2.在程序中连续两次调用该接口时,ksoap2在解析第二次调用返回的结果时抛异常。
? ? 异常信息如下:org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:0 in java.io.InputStreamReader@4383bf38)?
3.打断点调试时,不会出现该异常。

4.无奈之下使用android 的HttpURLConnection 直接调用webservice接口,直接使用时不会发生以上异常,所以使用ksoap2 访问webservice需要设置什么呢?

5.使用HttpUrlConnection访问webserivice代码如下:

(一)连接webservice

String?ServerUrl="webservice地址";
?String soapAction="http://www.v_card.net.cn/PhoneClient/GetVcardJson";

?String data="";

String requestData="<?xml version="1.0" encoding="utf-8"?>rn"+
"<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">rn"
??+"<soap:Header>rn"+
?? ?"<AuthHeader xmlns="http://www.v_card.net.cn/PhoneClient/">rn"+
?? ? "<UserId>"+userID+"</UserId>rn"+
?? ?"</AuthHeader>rn"+
??"</soap:Header>rn"+
??"<soap:Body>rn"+
?? ?"<GetVcardJson xmlns="http://www.v_card.net.cn/PhoneClient/">rn"+
?? ? ?"<vcardUserNo>"+userNo+"</vcardUserNo>rn"+
?? ?"</GetVcardJson>rn"+
??"</soap:Body>rn"+

"</soap:Envelope>";

try{
URL url =new URL(ServerUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
byte[] bytes=requestData.getBytes("utf-8");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","text/xml;charset=utf-8");
con.setRequestProperty("SOAPAction",soapAction);
con.setRequestProperty("Content-Length",""+bytes.length);
OutputStream outStream=con.getOutputStream();
outStream.write(bytes);
outStream.flush();
outStream.close();
InputStream inStream=con.getInputStream();

data=parser(inStream);

(二)解析返回的数据

private static String parser(InputStream in){ XmlPullParser parser=Xml.newPullParser(); String data=""; try{ int flag=0; parser.setInput(in,"utf-8"); int evenType=parser.getEventType(); while(evenType!=XmlPullParser.END_DOCUMENT){ switch(evenType){ case XmlPullParser.START_DOCUMENT:break; case XmlPullParser.START_TAG: break; case XmlPullParser.TEXT: data=parser.getText(); break; case XmlPullParser.END_TAG:break; } parser.next(); evenType=parser.getEventType(); } }catch(XmlPullParserException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } return data; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读