接口之---http接口

在和第三方公司进行接口的调用时,由于伙伴公司是使用http接口,所以开发了http接口进行调用

1.修改web.xml文件

[java]  view plain  copy
  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文件

[java]  view plain  copy
  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.       
  12.     private static final long serialVersionUID = 1L;  
  13.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  14.             throws ServletException, IOException {  
  15.   response.setContentType("text/html;charset=utf-8");  
  16.      request.setCharacterEncoding("utf-8");  
  17.      response.setCharacterEncoding("utf-8");  
  18.      PrintWriter out = response.getWriter();  
  19.       String content = request.getParameter("content");  
  20.        //String content = new String(request.getParameter("content").getBytes("iso-8859-1"), "utf-8");    
  21.         String mobiles = request.getParameter("mobiles");  
  22.         String businesscode = request.getParameter("businesscode");  
  23.         String businesstype = request.getParameter("businesstype");  
  24.         if (content == null || "".equals(content) || content.length() <= 0) {  
  25.          System.out.println("http call failed,参数content不能为空,程序退出");  
  26.         } else if (mobiles == null || "".equals(mobiles)  
  27.                 || mobiles.length() <= 0) {  
  28.          System.out.println("http call failed,参数mobiles不能为空,程序退出");  
  29.         } else {  
  30.          /*SendSMSServiceImpl send = new SendSMSServiceImpl();*/  
  31.          SendSMSAction sendSms = (SendSMSAction) SpringContextUtil.getBean("sendSMS");  
  32.          sendSms.sendSms(content, mobiles, businesscode, businesstype);  
  33.          System.out.println("---http call success---");  
  34.         }  
  35.         out.close();  
  36.     }  
  37.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  38.             throws ServletException, IOException {  
  39.         this.doGet(request, response);  
  40.     }  
  41. }  

3.调用http接口

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值