HttpCilent通过POST方式调用webservice服务

直接上代码

{
        String url="http://localhost:8083";
        InputStream is=null;
        BufferedReader bf=null;
        HttpClient client=new HttpClient();
        PostMethod method=new PostMethod(url+"/CertCheckService.asmx/CheckInfo");
        HttpMethodParams paras=method.getParams();
        paras.setContentCharset("utf-8");
        method.setRequestHeader("Host","localhost");
        method.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        //method.setRequestHeader("Content-Length",);
        //requestMsg= URLEncoder.encode(requestMsg,"utf-8");
        method.setParameter("json", requestMsg);
        String returnMsg="";
        try {
            client.executeMethod(method);
            is=method.getResponseBodyAsStream();
            bf=new BufferedReader(new InputStreamReader(is,"utf-8"));
            String tempStr=null;
            while((tempStr=bf.readLine())!=null){
                returnMsg+=tempStr;
            }
            bf.close();
            System.out.println(returnMsg);


        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bf!=null){
                try {
                    bf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return returnMsg;

    }

你可能感兴趣的:(HttpCilent通过POST方式调用webservice服务)