当前位置:   article > 正文

java 实现Get、Put、Delete方式发送xml格式数据的http协议_java get方式上传xml

java get方式上传xml

今天工作之余把最近实现的功能在记录一下,方便以后查找。

  • 导入需要的包
  1. <dependency>
  2. <groupId>org.apache.httpcomponents</groupId>
  3. <artifactId>httpclient</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.apache.httpcomponents</groupId>
  7. <artifactId>httpclient-cache</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>dom4j</groupId>
  11. <artifactId>dom4j</artifactId>
  12. </dependency>
  • 下面请求的url中需要用到服务端ip地址和端口号
  1.     //从配置文件中获取服务端ip地址
  2. @Value("${poc.server.ip}")
  3.     private String serverIp;
  4.     
  5.     private final String serverPort="8866";
  6.     
  7.     private final String userProfilePort="8070";
  • 封装Xml格式数据的内容,这个xml内容稍复杂。
  1. /**
  2. * @param groupNumber 后台生成的组号码
  3. * @param callerSsi 发起方号码
  4. * @return xml
  5. */
  6. private String getRequestBody(String groupNumber, String callerSsi){
  7. Document doc = DocumentHelper.createDocument();
  8. //创建根元素
  9. Element root = doc.addElement("group","urn:oma:xml:poc:list-service");
  10. //元素创建属性
  11. root.addAttribute("xmlns:rl", "urn:ietf:params:xml:ns:resource-lists").addNamespace("rl", "urn:ietf:params:xml:ns:resource-lists");
  12. root.addAttribute("xmlns:cp", "urn:ietf:params:xml:ns:common-policy").addNamespace("cp", "urn:ietf:params:xml:ns:common-policy");
  13. root.addAttribute("xmlns:ocp", "urn:oma:xml:xdm:common-policy").addNamespace("ocp", "urn:oma:xml:xdm:common-policy");
  14. root.addAttribute("xmlns:oxe", "urn:oma:xml:xdm:extensions").addNamespace("oxe", "urn:oma:xml:xdm:extensions");
  15. root.addAttribute("xmlns:mcpttgi", "urn:3gpp:ns:mcpttGroupInfo:1.0").addNamespace("mcpttgi", "urn:3gpp:ns:mcpttGroupInfo:1.0");
  16. //创建子元素
  17. Element listService = root.addElement("list-service","");
  18. listService.addAttribute("uri", "sip:"+groupNumber+"@mcptt.mcs.com");
  19. //给子元素创建子元素
  20. Element displayName = listService.addElement("display-name");
  21. displayName.addAttribute("xml:lang", "en-us");
  22. displayName.addText("test");
  23. listService.addElement("mcpttgi:on-network-invite-members").addText("true");
  24. listService.addElement("mcpttgi:on-network-max-participant-count").addText("10");
  25. Element cpRuleset = listService.addElement("cp:ruleset");
  26. Element cpRule = cpRuleset.addElement("cp:rule");
  27. cpRule.addAttribute("id", "a7c");
  28. Element cpConditions = cpRule.addElement("cp:conditions");
  29. cpConditions.addElement("is-list-member");
  30. Element cpActions = cpRule.addElement("cp:actions");
  31. cpActions.addElement("allow-initiate-conference").addText("false");
  32. cpActions.addElement("join-handling").addText("false");
  33. cpActions.addElement("mcpttgi:allow-MCPTT-emergency-call").addText("true");
  34. cpActions.addElement("mcpttgi:allow-imminent-peril-call").addText("true");
  35. Element supServices = listService.addElement("oxe:supported-services");
  36. Element oxeService = supServices.addElement("oxe:service");
  37. oxeService.addAttribute("enabler", "urn:urn-7:3gpp-service.ims.icsi.mcptt");
  38. Element groupMedia = oxeService.addElement("oxe:group-media");
  39. groupMedia.addElement("mcpttgi:mcptt-speech");
  40. listService.addElement("on-network-disabled").addText("false");
  41. listService.addElement("on-network-group-priority").addText("0");
  42. listService.addElement("owner").addText(callerSsi);
  43. return doc.asXML();
  44. }
  • 发送Get方式的请求,请求参数在url中传递
  1. /**
  2. * @param type 呼叫类型
  3. * @param callerSsi 发起方号码
  4. */
  5. public void getUserProfile(String type,String callerSsi) {
  6. CloseableHttpResponse httpResponse =null;
  7. String urlType="";
  8. String xmlType="";
  9. String gppType="";
  10. try {
  11. if("mcptt".equals(type)) {
  12. urlType="@mcptt.mcs.com";
  13. xmlType="/mcptt-user-profile.xml";
  14. gppType="mcptt";
  15. }else if("mcdata".equals(type)) {
  16. urlType="@mcdata.mcs.com";
  17. xmlType="/mcdata-user-profile.xml";
  18. gppType="mcdata";
  19. }else if("mcvideo".equals(type)) {
  20. urlType="@mcvideo.mcs.com";
  21. xmlType="/mcvideo-user-profile.xml";
  22. gppType="mcvideo";
  23. }
  24. URI uri = new URI("http://"+serverIp+":"+userProfilePort+"/mcs/sip:"+callerSsi+urlType+"//mcs/sip:"+callerSsi+urlType+
  25. "/org.3gpp."+gppType+".user-profile/users/sip:"+callerSsi+urlType+xmlType);
  26. HttpGet httpGet = new HttpGet(uri);
  27. httpGet.setHeader("Content-Type", "text/xml");
  28. httpGet.addHeader("User-Agent", "XDM-client/OMA2.1");
  29. CloseableHttpClient httpClient = HttpClients.createDefault();
  30. httpResponse = httpClient.execute(httpGet);
  31. //获取返回的结果信息
  32. String resultMsg = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
  33. //获取响应码
  34. int statusCode = httpResponse.getStatusLine().getStatusCode();
  35. logger.info("Response UserProfile statusCode:{};type:{};Content:{};",statusCode,type,resultMsg);
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. logger.error("Response UserProfile Error!",e);
  39. }
  40. }
  • 发送Put方式的请求
  1. public void updateGroupData() {
  2. CloseableHttpResponse httpResponse =null;
  3. String groupNumber="600105";
  4. String callerSsi="800101";
  5. try {
  6. String urlType="@mcvideo.mcs.com";
  7. String requestBody = getRequestBody(groupNumber, callerSsi);
  8. URI uri = new URI("http://"+serverIp+":"+serverPort+"/path1/org.openmobilealliance.groups/users/sip:"+callerSsi+urlType+"/groupdocument1.xml&operate=modify");
  9. HttpPut httpPut = new HttpPut(uri);
  10. httpPut.setHeader("Content-Type", "text/xml");
  11. httpPut.addHeader("User-Agent", "okhttp/3.6.0");
  12. httpPut.setEntity(new StringEntity(requestBody,"UTF-8"));
  13. CloseableHttpClient httpClient = HttpClients.createDefault();
  14. httpResponse = httpClient.execute(httpPut);
  15. int code = httpResponse.getStatusLine().getStatusCode();
  16. logger.info("ModifyGroup SendRequest End code:{};groupNumber:{};",code,groupNumber);
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. logger.error("ModifyGroup Error!",e);
  20. }
  21. }
  • 发送Delete方式的请求,Delete方式不能直接在Entity中封装参数,需要实现一个自定义的类继承http的基类

       自定义的Http请求的Delete类

  1. public class HttpDeleteMethod extends HttpEntityEnclosingRequestBase{
  2. public final static String METHOD_NAME = "DELETE";
  3. @Override
  4. public String getMethod() {
  5. return METHOD_NAME;
  6. }
  7. public HttpDeleteMethod() {
  8. super();
  9. }
  10. public HttpDeleteMethod(final URI uri) {
  11. super();
  12. setURI(uri);
  13. }
  14. /**
  15. * @throws IllegalArgumentException if the uri is invalid.
  16. */
  17. public HttpDeleteMethod(final String uri) {
  18. super();
  19. setURI(URI.create(uri));
  20. }
  21. }

实现Delete方式封装参数发送

  1. public int deleteGroup() {
  2. CloseableHttpResponse httpResponse =null;
  3. String groupNumber="600105";
  4. String callerSsi="800101";
  5. try {
  6. URI uri = new URI("http://"+serverIp+":"+serverPort+"/path1/org.openmobilealliance.groups/users/sip:"+callerSsi+"@mcptt.mcs.com/groupdocument1.xml");
  7. HttpDeleteMethod httpDelete = new HttpDeleteMethod(uri);
  8. httpDelete.setHeader("Content-Type", "text/xml");
  9. httpDelete.addHeader("User-Agent", "okhttp/3.6.0");
  10. //封装body内容
  11. Document doc = DocumentHelper.createDocument();
  12. Element root = doc.addElement("group","urn:oma:xml:poc:list-service");
  13. root.addAttribute("xmlns:rl", "urn:ietf:params:xml:ns:resource-lists").addNamespace("rl", "urn:ietf:params:xml:ns:resource-lists");
  14. root.addAttribute("xmlns:cp", "urn:ietf:params:xml:ns:common-policy").addNamespace("cp", "urn:ietf:params:xml:ns:common-policy");
  15. root.addAttribute("xmlns:ocp", "urn:oma:xml:xdm:common-policy").addNamespace("ocp", "urn:oma:xml:xdm:common-policy");
  16. root.addAttribute("xmlns:oxe", "urn:oma:xml:xdm:extensions").addNamespace("oxe", "urn:oma:xml:xdm:extensions");
  17. root.addAttribute("xmlns:mcpttgi", "urn:3gpp:ns:mcpttGroupInfo:1.0").addNamespace("mcpttgi", "urn:3gpp:ns:mcpttGroupInfo:1.0");
  18. Element listService = root.addElement("list-service","");
  19. listService.addAttribute("uri", "sip:"+groupNumber+"@group.com");
  20. String requestBody=doc.asXML();
  21. httpDelete.setEntity(new StringEntity(requestBody,"UTF-8"));
  22. CloseableHttpClient httpClient = HttpClients.createDefault();
  23. httpResponse = httpClient.execute(httpDelete);
  24. //String resultMsg = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
  25. int statusCode = httpResponse.getStatusLine().getStatusCode();
  26. logger.info("DeleteGroup statusCode:{};groupNumber:{};",statusCode,groupNumber);
  27. return statusCode;
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. logger.error("DeleteGroup Error!",e);
  31. return -1;
  32. }
  33. }

好了,这样三种方式就实现完了。需要注意的细节就是Delete方式传递参数需要自己实现。封装xml格式的时候添加完属性一定要加namespace,不然封装的xml格式就不对了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/314193
推荐阅读
相关标签
  

闽ICP备14008679号