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();
}

解决方案 »

  1.   

    好像意思理解错了。
    我需要如何接收xml报文并返回结果报文的方法。
    谢谢。
      

  2.   

    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>");