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();
}
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();
}
解决方案 »
- 一个关于velocity,*.vm页面显示的问题,求高手,在线等。。。。
- 初学JSF,出现的问题。
- aop实现日志报错No setter found for property 'proxyinterface' in class
- 关于缓存的使用
- 请教:读取.xml异常 多谢。
- 为什么书上源代码里Struts1.1里Action的saveErrors可以保存ActionMessage的错误对象?
- 来者有分,探讨关于struts的数据库问题!
- 怎样在jb9中配置struts1.1
- spring结合quartz启动报错
- 用mina实现websocket服务端,是否需要处理粘包和断包问题
- 上传文件<html:file>,那么form中用来接收文件的数据成员是什么类型?
- Myeclipse里启动Weblogic时Struts-config报错 救命~~
我需要如何接收xml报文并返回结果报文的方法。
谢谢。
------------------------------
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>");