httpclient

之前服务调用一用过直是用的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("")+"".length(),result.indexOf(""));
		System.out.println(code);
		return code;

// 获取xml
	public String getXMLString(Integer channelid, Integer customerid, long batchid, Integer mediatype, String media,
			String tels, String auth) {
//		String XML_HEADER = "";
		StringBuffer sb = new StringBuffer();
//		sb.append(XML_HEADER);
		sb.append("");
		sb.append("    " + a+ "");
		sb.append("    " + b+ "");
		sb.append("    " + c+ "");
		sb.append("    " + d+ "");
		sb.append("    " + e+ "");
		sb.append("    " + f+ "");
		sb.append("    " + g+ "");
		sb.append("");
		// 返回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);
    }



你可能感兴趣的:(httpclient)