求助。如何写这个webservice?

cdmika 2007-02-23 12:50:55
菜鸟问题,如下为http的请求应答报文
http请求格式如下,
PosT /example HTTP/1.1
Host: example.com
Content-Type: text/xml
Content-length: 870

<?xml version="1.0" encoding="GBK"?>
<operation_in type="struct">
<service_name type="string">cc_userpwd_verify</service_name>
</operation_in>
返回格式如下:
HTTP/1.1 200 OK
Connection: close
Content-Length: 682
Content-Type: text/xml

<?xml version="1.0" encoding="GBK"?>
<operation_out type="struct">
<service_name type="string">cc_userpwd_verify</service_name>
</operation_out>
如何编写JAVA或者jsp代码来发送请求和接收应答?
急用,谢谢!!!
...全文
343 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
会飞的小洋洋 2007-03-06
  • 打赏
  • 举报
回复
...
roy007 2007-03-05
  • 打赏
  • 举报
回复
jsp页面上写这段就能让你接收到发过来的数据包了
------------------------------
byte recv[] = new byte[1024];/*enough length for post*/
int readLen = 0; /*every time get bytes*/
int totalLen = 0; /*total get bytes*/

InputStream is = null;
try{
is = request.getInputStream();
while(is!=null && totalLen<1024 && (readLen = is.read(recv,totalLen,64))!=-1){
totalLen += readLen;
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{is.close();} catch(Exception e){}
}
String getString = new String(recv).trim();
---------------------------------
然后用xml dom来提取你的数据(这是正规的方法,简单一点也可以用正则表达式,再简单点比如你的例子,直接用字符串的indexOf方法取出来也可以)
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream ism = streamFromString(getString);
Document document = builder.parse(ism);
NodeList nodes_service_name= document.getElementsByTagName("service_name");
String service_name = nodes_service_name.item(0).getAttributes().item(0).getNodeValue().toString();
-----------------------------------
然后包装要发送的东西后直接out.print人家就能收到了
out.print("<operation_out type=\"struct\">
<service_name type=\"string\">cc_userpwd_verify</service_name>
</operation_out>");
cdmika 2007-02-28
  • 打赏
  • 举报
回复
好像意思理解错了。
我需要如何接收xml报文并返回结果报文的方法。
谢谢。
mickey_uuu 2007-02-27
  • 打赏
  • 举报
回复
给你个代码片断参考
mickey_uuu 2007-02-27
  • 打赏
  • 举报
回复
String urlstr="your service url";
try {
URL url = new URL(urlstr);
URLConnection conn = url.openConnection();
conn.setDoInput(true); // RTS: read & write
conn.setDoOutput(true); //
conn.setUseCaches(false);
conn.setRequestProperty("Method","POST");
conn.setRequestProperty("Content-type",
"content-type=text/xml; charset=UTF-8");

OutputStream out=conn.getOutputStream();
//你的请求xml
String requeststr="<?xml version=\"1.0\" encoding=\"GBK\"?>
<operation_in type=\"struct\">
<service_name type=\"string\">cc_userpwd_verify</service_name>
</operation_in>
";
out.write(requeststr.getBytes());
// p.flush();
out.flush();
byte buffer[]=new byte[1024];
int len=0;
InputStream is=conn.getInputStream();

while((len=is.read(buffer))>0){
System.out.write(buffer,0,len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cdmika 2007-02-26
  • 打赏
  • 举报
回复
如果没有wsdl呢
Rick_ang 2007-02-24
  • 打赏
  • 举报
回复
找到wsdl,用WSDL2Java最方便
cdmika 2007-02-24
  • 打赏
  • 举报
回复
谢谢各位帮忙解答啊。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧