Rectangle 2015-10-09 08:32 采纳率: 0%
浏览 2055

求教一个向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条回答 默认 最新

  • wangweilica6 2015-10-10 07:25
    关注

    加上转义符“\”试试

    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制