求教一个向Webservice传递参数被转码的错误!

我用下面的方式调用一个webservice的方法,传递的strWhere这个参数里面写了个字段模糊匹配的sql语句" strWhere=IDCARD LIKE '%1234567890%' ",在将该参数向webservice传递过去后,webservice那边收到的字符串是将该字符串里面的第一个“%”和后面紧跟着的两位数字进行了转义,显示为“IDCARD LIKE '34567890%'”,其中的不可显示字符,其实是ascii码为12的字符!也就是说这样调用,“%”和后面紧跟的“12”被转义成一个字节了,本人刚学java,对这些编码转换的流程也搞不懂,一时不知道问题出在哪里,贴出代码,往各位大侠指导一下!谢谢!

public static void main(String[] args) {
// TODO Auto-generated method stub
// 获取WebService数据
String strWhere = "strWhere=IDCARD LIKE '%1234567890%13'";
String s3 = HttpUtil.doPost("http://localhost:1739/bill.asmx",
"HelloWorld", strWhere,
"application/x-www-form-urlencoded;charset=utf-8;");

    System.out.println(s3);
    System.exit(0);

}

public static String doPost(String ipStr, String url, String strWhere,
        String contentType) {
    String resultLine;
    OutputStream outputStream = null;
    OutputStreamWriter outputStreamWriter = null;

    System.out.println("sql=" + strWhere);

    InputStream inputStream = null;
    InputStreamReader inputStreamReader = null;
    BufferedReader bufferedReader = null;
    StringBuffer stringBuffer = new StringBuffer();
    try {
        String strUrl = ipStr + "/" + url;
        URL realUrl = new URL(strUrl);
        URLConnection connection = realUrl.openConnection();
        HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("content-type", contentType);
        outputStream = httpURLConnection.getOutputStream();
        outputStreamWriter = new OutputStreamWriter(outputStream);
        System.out.println("strWhere=" + strWhere);
        outputStreamWriter.write(strWhere);// ,0,strWhere.length()
        outputStreamWriter.flush();

        if (httpURLConnection.getResponseCode() >= 300) {
            throw new Exception(
                    "Http Request is not success,Response code is "
                            + httpURLConnection.getResponseCode());

        }
        inputStream = httpURLConnection.getInputStream();
        inputStreamReader = new InputStreamReader(inputStream, "utf-8");
        bufferedReader = new BufferedReader(inputStreamReader);
        while ((resultLine = bufferedReader.readLine()) != null) {
            System.out.println("返回的行=" + resultLine);
            if (!resultLine.trim().equals("")) {
                stringBuffer.append(resultLine).append("\n"); // 读完每行后换行
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return stringBuffer.toString();
}

2个回答

加上转义符“\”试试

UrlEncode两次后传输,服务端UrlDecode一次(适用于tomcat,因为tomcat会自动decode一次)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问