java httpserver 接口_java创建http接口

1.修改web.xml文件

TestHTTPServer

com.atoz.http.SmsHTTPServer

TestHTTPServer

/httpServer

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 = "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();

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
创建一个JavaHttpServer,你可以使用Java自带的HttpServer类。下面是一个创建HttpServer的示例代码: ```java import java.io.IOException; import java.net.InetSocketAddress; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import com.sun.net.httpserver.HttpServer; public class ThreadHttpServer { private static final int port = 8880; private static final String Httpcontext = "/demo"; private static final int nThreads = 8; public static void main(String\[\] args) { HttpServer httpServer; try { httpServer = HttpServer.create(new InetSocketAddress(port), 0); httpServer.createContext(Httpcontext, new HttpHandlerDemo()); ExecutorService executor = Executors.newFixedThreadPool(nThreads); httpServer.setExecutor(executor); httpServer.start(); System.out.println("启动端口:" + port); System.out.println("根节点:" + Httpcontext); System.out.println("并发数:" + nThreads); } catch (IOException e) { e.printStackTrace(); } } } ``` 这个示例代码创建了一个HttpServer实例,并指定了监听的端口号和根路径。同时,还设置了并发数来控制处理请求的线程池大小。你可以根据自己的需求修改这些参数。注意,这个示例代码使用了com.sun.net.httpserver.HttpServer类,所以你需要确保你的项目中引入了相关的依赖。 #### 引用[.reference_title] - *1* *2* [java 自带HttpServer创建接口服务端](https://blog.csdn.net/xiaolenglala/article/details/102965011)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Java实现http服务器](https://blog.csdn.net/fenglllle/article/details/118142770)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值