httpclient

来源:互联网 发布:网络作家怎样赚钱 编辑:程序博客网 时间:2024/06/05 08:07

之前服务调用一用过直是用的dubbo,没有用过httpclient

这次试用感觉还是很简单的,上手简单,可能深入理解就没有那么简单了,下面简单写下上手使用的小demo

// 发送xml数据到服务HttpClientUtil httpClientUtil = new HttpClientUtil();String result = httpClientUtil.sendHttpPost("httpurl", outbound.getXMLString(a, b, c, d, e, f, g));System.out.println(result);String code = result.substring(result.indexOf("<statuscode>")+"<statuscode>".length(),result.indexOf("</statuscode>"));System.out.println(code);return code;

// 获取xmlpublic String getXMLString(Integer channelid, Integer customerid, long batchid, Integer mediatype, String media,String tels, String auth) {//String XML_HEADER = "<?xml version=\"1.0\"?>";StringBuffer sb = new StringBuffer();//sb.append(XML_HEADER);sb.append("<Request>");sb.append("    <a>" + a+ "</a>");sb.append("    <b>" + b+ "</b>");sb.append("    <c>" + c+ "</c>");sb.append("    <d>" + d+ "</d>");sb.append("    <e>" + e+ "</e>");sb.append("    <f>" + f+ "</f>");sb.append("    <g>" + g+ "</g>");sb.append("</Request>");// 返回String格式return sb.toString();}
工具类

 /**     * 发送 post请求     *     * @param httpUrl 地址     * @param params  参数(格式:key1=value1&key2=value2)     */    public String sendHttpPost(String httpUrl, String params) {        HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost        try {            //设置参数            StringEntity stringEntity = new StringEntity(params, "UTF-8");            stringEntity.setContentType("application/xml");            httpPost.setEntity(stringEntity);        } catch (Exception e) {            e.printStackTrace();        }        return sendHttpPost(httpPost);    }



0 0
原创粉丝点击