java调用http api接口(java创建http接口及调用)

1.修改web.xml文件

  1. <servlet>
  2. <servlet-name>TestHTTPServer</servlet-name>
  3. <servlet-class>com.atoz.http.SmsHTTPServer</servlet-class>
  4. </servlet>
  5. <servlet-mapping>
  6. <servlet-name>TestHTTPServer</servlet-name>
  7. <url-pattern>/httpServer</url-pattern>
  8. </servlet-mapping>

2.新建SmsHTTPServer.java文件

  1. package com.atoz.http;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletrequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import com.atoz.action.order.SendSMSAction;
  9. import com.atoz.util.SpringContextUtil;
  10. public class SmsHTTPServer extends HttpServlet {
  11. private static final long serialVersionUID = 1L;
  12. public void doGet(HttpServletRequest request, HttpServletResponse response)
  13. throws ServletException, IOException {
  14. response.setContentType("text/html;charset=utf-8");
  15. request.setCharacterEncoding("utf-8");
  16. response.setCharacterEncoding("utf-8");
  17. PrintWriter out = response.getWriter();
  18. String content = request.getParameter("content");
  19. //String content = new String(request.getParameter("content").getBytes("iso-8859-1"), "utf-8");
  20. String mobiles = request.getParameter("mobiles");
  21. String businesscode = request.getParameter("businesscode");
  22. String businesstype = request.getParameter("businesstype");
  23. if (content == null || "".equals(content) || content.length() <= 0) {
  24. System.out.println("http call failed,参数content不能为空,程序退出");
  25. } else if (mobiles == null || "".equals(mobiles)
  26. || mobiles.length() <= 0) {
  27. System.out.println("http call failed,参数mobiles不能为空,程序退出");
  28. } else {
  29. /*SendSMSServiceImpl send = new SendSMSServiceImpl();*/
  30. SendSMSAction sendSms = (SendSMSAction) SpringContextUtil.getBean("sendSMS");
  31. sendSms.sendSms(content, mobiles, businesscode, businesstype);
  32. System.out.println("---http call success---");
  33. }
  34. out.close();
  35. }
  36. public void doPost(HttpServletRequest request, HttpServletResponse response)
  37. throws ServletException, IOException {
  38. this.doGet(request, response);
  39. }
  40. }

3.调用http接口

  1. String content = "测试";
  2. content = URLEncoder.encode(content, "utf-8");
  3. String url = "http://localhost:8180/atoz_2014/httpServer?content=" content "&mobiles=15301895007";
  4. URL httpTest;
  5. try {
  6. httpTest = new URL(url);
  7. BufferedReader in;
  8. try {
  9. in = new BufferedReader(new InputStreamReader(
  10. httpTest.openStream()));
  11. String inputLine = null;
  12. String resultMsg = null;
  13. //得到返回信息的xml字符串
  14. while ((inputLine = in.readLine()) != null)
  15. if(resultMsg != null){
  16. resultMsg = inputLine;
  17. }else {
  18. resultMsg = inputLine;
  19. }
  20. in.close();
  21. } catch (MalformedURLException e) {
  22. e.printStackTrace();
  23. }
  24. } catch (IOException e) {
  25. // TODO Auto-generated catch block
  26. e.printStackTrace();
  27. }

java调用http api接口(java创建http接口及调用)(1)

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页