日期:2014-05-18  浏览次数:20670 次

axis2开发的webservice可以直接返回字符串吗
最近使用axis2发布webservice,以前也没注意到这个问题,就是有的时候想直接返回一个字符串,但是在浏览器敲入地址显示出来的却是xml如下所示。
<ns:getVersionResponse><ns:return>Hi - the Axis2 version is 1.6.2</ns:return></ns:getVersionResponse>


自己就想直接返回一个字符串,也省了解析xml的工作,下面是我用HTTPConnection调用webservice的代码。
package com.tsp.webservice;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.imageio.ImageIO;

import org.dom4j.*;

public class TestService {
public static void main(String args[]) throws Exception {
Map<String, String> m = new HashMap<String, String>();
try {
System.out.println(loginCheck(m));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static String loginCheck(Map<String, String> parameter)
throws Exception {
// 拼接参数,这里最好使用StringBuilder

// StringBuilder builder = new StringBuilder();
// for (Map.Entry<String, String> entry : parameter.entrySet()) {
// // 这里的参数需要编码
// builder.append(entry.getKey() + "=").append(
// URLEncoder.encode(entry.getValue(), "UTF-8") + "&");
//
// }
// // 除去多余的&
// builder.deleteCharAt(builder.length() - 1);
// System.out.println(builder.toString());
// byte[] b2 = builder.toString().getBytes();
// 封装数据,数据以byte方式传输
// byte[] b = builder.toString().getBytes();
// 需要请求的连接
URL url = new URL(
"http://localhost:8080/axis2/services/Version/getVersion");
// 打开连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置请求方式和消息头以及超时时间
conn.setRequestMethod("POST");
conn.setConnectTimeout(10000);
// 设置是否允许对外输出数据
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(0));
// conn.setRequestProperty("Content-Length", "0");

// OutputStream outPut = conn.getOutputStream();
//
// outPut.write(b);
//
// outPut.close();

// 判断请求是否成功
if (conn.getResponseCode() == 200) {
InputStream in=conn.getInputStream();  
byte[] b2=new byte[1024];  
int len=0;  
int temp=0;  
while((temp=in.read())!=-1){  
    b2[len]=(byte)temp;  
&nb