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("<statuscode>")+"<statuscode>".length(),result.indexOf("</statuscode>"));
		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 = "<?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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值