> 馆藏中心

java 接口 http

来源:cnblogs 编辑:张华

1.修改web.xml文件模拟HTTP的调用,写的一个http接口-><servlet><servlet-name>TestHTTPServer</servlet-name><servlet-class>com.atoz.http.SmsHTTPServer</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestHTTPServer</servlet-name> <url-pattern>/httpServer</url-pattern> </servlet-mapping>2.新建SmsHTTPServer.java文件packagecom.atoz.http;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;importcom.atoz.action.order.SendSMSAction;importcom.atoz.util.SpringContextUtil;public class SmsHTTPServer extends HttpServlet { private static final long serialVersionUID=1L;public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException { response.setContentType("text/html;charset=utf-8");request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");PrintWriter out=response.getWriter();String content=request.getParameter("content");String content=new String(request.getParameter("content").getBytes("iso-8859-1"),"utf-8");String mobiles=request.getParameter("mobiles");String businesscode=request.getParameter("businesscode");String businesstype=request.getParameter("businesstype");if(content=null|"".equals(content)|content.length()){ System.out.println("http call failed,参数content不能为空,程序退出");} else if(mobiles=null|"".equals(mobiles)|mobiles.length()){ System.out.println("http call failed,参数mobiles不能为空,程序退出");} else {/*SendSMSServiceImpl send=new SendSMSServiceImpl();SendSMSAction sendSms=(SendSMSAction)SpringContextUtil.getBean("sendSMS");sendSms.sendSms(content,mobiles,businesscode,businesstype);System.out.println("-http call success---");} out.close();}public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException { this.doGet(request,response);} }3.调用http接口String content="测试;content=URLEncoder.encode(content,"utf-8");String url="http://localhost:8180/atoz_2014/httpServer?content=" + content + "&mobiles=15301895007";URL httpTest;try { httpTest=new URL(url);BufferedReader in;try { in=new BufferedReader(new InputStreamReader(httpTest.openStream()));String inputLine=null;String resultMsg=null;得到返回信息的xml字符串 while((inputLine=in.readLine())!null)if(resultMsg!null){ resultMsg+inputLine;}else { resultMsg=inputLine;} in.close();} catch(MalformedURLException e){ e.printStackTrace();} } catch(IOException e){/TODO Auto-generated catch block e.printStackTrace();}打字不易,望采纳,谢谢www.179s.com防采集请勿采集本网。

@ControllerAdvicepublic class ErrorHandler {@ExceptionHandler(value = HttpRequestMethodNotSupportedExceptio.class)public ResponseEntity httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException exception,HttpServletRequest request) { // 返回自定义的ResponseEntity即可}}收获园豆:10那应该不是浏览器报错吧,是你后端没处理这种方法,你后端根据请求的方法执行不同的逻辑不就可以了吗

import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;public class DemoTest1 {public static final String GET_URL="http://112.4.27.9/mall-back/if_user/store_list?storeId=32";public static final String POST_URL="http://112.4.27.9/mall-back/if_user/store_list";妙兜测试接口public static final String POST_URL="http://121.40.204.191:8180/mdserver/service/installLock";接口调用 GETpublic static void httpURLConectionGET(){try {URL url=new URL(GET_URL);把字符串转换为URL请求地址HttpURLConnection connection=(HttpURLConnection)url.openConnection();打开连接connection.connect();连接会话获取输入流BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));String line;StringBuilder sb=new StringBuilder();while((line=br.readLine())!null){/循环读取流sb.append(line);}br.close();关闭流connection.disconnect();断开连接System.out.println(sb.toString());} catch(Exception e){e.printStackTrace();System.out.println("失败!}}接口调用 POSTpublic static void httpURLConnectionPOST(){try {URL url=new URL(POST_URL);将url 以 open方法返回的urlConnection 连接强转为HttpURLConnection连接(标识一个url所引用的远程对象连接)HttpURLConnection connection=(HttpURLConnection)url.openConnection();此时cnnection只是为一个连接对象,待连接中设置连接输出流为true,默认false(post 请求是以流的方式隐式的传递参数)connection.setDoOutput(true);设置连接输入流为trueconnection.setDoInput(true);设置请求方式为postconnection.setRequestMethod("POST");post请求缓存设为falseconnection.setUseCaches(false);设置该HttpURLConnection实例是否自动执行重定向connection.setInstanceFollowRedirects(true);设置请求头里面的各个属性(以下为设置内容的类型,设置为经过urlEncoded编码过的from参数)application/x-javascript text/xml->xml数据 application/x-javascript->json对象 application/x-www-form-urlencoded->表单数据charset=utf-8 必须要,不然妙兜那边会出现乱码【★】connection.setRequestProperty("Content-Type内容来自www.179s.com请勿采集。

本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。本文《java 接口 http》转载自cnblogs,版权归原作者所有,若侵权请联系:E-MAIL:513175919@qq.com
违法违规信息请立即联系本网可获得现金奖励,TEL:1-8-2-1-0-2-3-3-3-8-1(电话仅供违法违规信息举报,侵权类信息请EMAIL。)

www.179s.com false 互联网 http://www.179s.com/jsblogcnk/po/pohdaa.html report 4898 @ControllerAdvicepublic class ErrorHandler {@ExceptionHandler(value = HttpRequestMethodNotSupportedExceptio.class)public ResponseEntity httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException exception,HttpServletRequest requ

热门图片

经济金融企业管理法律法规社会民生科学教育降生活体育运动文化艺术电子数码电脑网络娱乐休闲行政地区心理分析医疗卫生