调用接口http封装

本文介绍了一个用于处理HTTP请求的方法httpHandler,该方法接受URL和XML字符串作为参数,并通过64位加密发送请求,然后接收并返回解密后的响应数据。

public static String httpHandler(String URL,String xml){

  try {
    URL url=new URL(URL);
    URLConnection con=url.openConnection();
    con.setDoOutput(true);
    OutputStreamWriter out=new OutputStreamWriter(con.getOutputStream());
    BASE64Encoder b = new BASE64Encoder();
    String request = b.encode(xml.toString().getBytes("utf-8")); //64位加密
    out.write(new String(request.getBytes()));
    out.flush();
    out.close();
    BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
    String line = "";
    String resultStr = "";
    for(line=br.readLine();line!=null;line=br.readLine()) {
      //resultStr += line +"\r\n";
      resultStr += new String(line.getBytes()) +"\r\n";//utf-8转换
    }
    BASE64Decoder c = new BASE64Decoder();
    return new String(c.decodeBuffer(resultStr),"utf-8");
  }catch(Exception e){
    e.printStackTrace();
    return e.getMessage();
  }
}

转载于:https://www.cnblogs.com/dmyglf/p/8119078.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值