java简易http的简单介绍

博主:adminadmin 2023-03-22 11:15:06 777

本篇文章给大家谈谈java简易http,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 如何搭建http服务器

看你具体是想做什么,现在现成的开源的java的http服务器有很多,像tomcat之类的都有http服务器功能,如果你只是单纯的需要用的话,直接用tomcat就好了

但是如果你是做要自己用java实现一个http服务器的话就要稍微麻烦一点

http服务器,本质上还是基于tcpip协议的服务器,首先用java的ServerSocket监听一个端口(也可以使用开源的server组件,如quickserver之类的),然后对客户端发上来的数据进行处理,这里就需要了解一下http协议了,因为上来的数据,都是按照http协议来组织的,你需要将请求数据解析后,将响应数据组织成http的响应,发回给客户端。这样一个简单的http服务器就实现了。

但是这个请求和响应都有很多种类,一个完整的http服务器应该要都能够支持,所以这里面的工作量还是有一点的。

另外,上面说的http服务器只是一个静态的服务器,如果你想让你写的服务具有动态功能,那你的服务器还得提供javaee的容器功能,这样做下去,没准你也能写一个tomcat出来了……

java 如何实现 http协议传输

Java 6 提供了一个轻量级的纯 Java Http 服务器的实现。下面是一个简单的例子:

public static void main(String[] args) throws Exception{

 HttpServerProvider httpServerProvider = HttpServerProvider.provider();

 InetSocketAddress addr = new InetSocketAddress(7778);

 HttpServer httpServer = httpServerProvider.createHttpServer(addr, 1);

 httpServer.createContext("/myapp/", new MyHttpHandler());

 httpServer.setExecutor(null);

 httpServer.start();

 System.out.println("started");

}

static class MyHttpHandler implements HttpHandler{

 public void handle(HttpExchange httpExchange) throws IOException {

String response = "Hello world!";

httpExchange.sendResponseHeaders(200, response.length());

OutputStream out = httpExchange.getResponseBody();

out.write(response.getBytes());

out.close();

 }

}

然后,在浏览器中访问

java如何创建一个简单的http接口?

1.修改web.xml文件

!-- 模拟HTTP的调用,写的一个http接口 -- servlet servlet-nameTestHTTPServer/servlet-name servlet-classcom.atoz.http.SmsHTTPServer/servlet-class /servlet servlet-mapping servlet-nameTestHTTPServer/servlet-name url-pattern/httpServer/url-pattern /servlet-mapping

2.新建SmsHTTPServer.java文件

package com.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;

import com.atoz.action.order.SendSMSAction; import com.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() = 0) { System.out.println("http call failed,参数content不能为空,程序退出"); } else if (mobiles == null || "".equals(mobiles) || mobiles.length() = 0) { 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 = "" + 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(); }

打字不易,望采纳,谢谢

关于java简易http和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。