需求-向第三方发送请求

  • 此次需求,需要给第三方发送一个参数格式为xml的数据,需先对准备好的数据进行格式化
    1. 对数据进行格式化,采用的是网上的转换方法
public static String beanToXml(Object obj,Class<?> load) throws JAXBException{
       JAXBContext context = JAXBContext.newInstance(load);
       Marshaller marshaller = context.createMarshaller();
//        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);格式化xml
       marshaller.setProperty(Marshaller.JAXB_ENCODING, "GBK");
       StringWriter writer = new StringWriter();
       marshaller.marshal(obj,writer);
       return writer.toString();
   }
  • 第二个是需要发送post、get请求等等
    CloseableHttpClient httpClient = HttpClients.createDefault();
  • 设置请求的超时时间
    RequestConfig customConfig = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).build();//配置超时时间为5S

posted on 2023-02-06 17:51  QAAAQ  阅读(10)  评论(0编辑  收藏  举报

导航