java 通过HTTP的方式调用Action

简介: java 通过HTTP的方式调用Action

用SSH框架写了一个微信项目,因为要和别的项目对接接口,本来想用WebService来实现,后来看到别的框架里面直接通过Actio来实现对接,所以就想到了用Action作为接口来实现WebService功能,通过HTTP来调用。代码如下。

Action代码:
public String testService() throws IOException, ClassNotFoundException{
//创建request和response对象
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request=ServletActionContext.getRequest();
//设置response编码
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
//创建writer实例
PrintWriter out = null;
out = response.getWriter();
//gson 用于把map转为JSON
Gson gs = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
//通过request获取传过来的参数,然后解析数据流获取参数
int length = (int) request.getContentLength();// 获取长度
InputStream is = request.getInputStream();
if (length != -1) {
byte[] data = new byte[length];
byte[] temp = new byte[512];
int readLen = 0;
int destPos = 0;
while ((readLen = is.read(temp)) > 0) {
System.arraycopy(temp, 0, data, destPos, readLen);
destPos += readLen;
}
//获取的参数
String result = new String(data, "UTF-8"); // utf-8编码
System.out.println(result);
}
//把要返回的参数写入map,转成JSON
Map map = new HashMap();
map.put("ID","123");
map.put("success", "true");
String jsonmap = gs.toJson(map);
out.print(jsonmap);
return null;
}

通过HTTP调用的代码:
public static void main(String[] args)
throws IOException, JSONException
{
//实例gson用于转换
Gson gs = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
//参数
Map map = new HashMap();
map.put("ID", "123123");
String jsonmap = gs.toJson(map);
String str = null;
//通过HTTPPost方式
try {
str = HttpsPost.send("http://localhost:8080/wx_manager/weixin/business_testService.do", "POST", jsonmap);
}
catch (IOException e) {
e.printStackTrace();
}
System.out.println(str);
以上是Action和调用Action的方法,因为是我写的一个小demo,所以没有真实的数据。
通过执行main方法返回了:
{ "ID": "123", "success": "true"} 这个就是我在Action里定义的返回数据了。
下面在把HTTP调用的方法代码贴出来:
public static String send(String urlString, String method,
String parameters)
throws IOException {
HttpURLConnection urlConnection = null;

URL url = new URL(urlString);

urlConnection = (HttpURLConnection) url.openConnection();

urlConnection.setRequestMethod(method);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("content-type", "text/html;charset=utf-8");
urlConnection.getOutputStream().write(parameters.getBytes("UTF-8"));
urlConnection.getOutputStream().flush();
urlConnection.getOutputStream().close();

//读取返回的流
InputStream input=urlConnection.getInputStream();
InputStreamReader inpurread=new InputStreamReader(input,"utf-8");
BufferedReader br=new BufferedReader(inpurread);
String a;
StringBuffer s=new StringBuffer();
while ((a=br.readLine())!=null) {
s.append(a);
}
return s.toString();
}

以上就是完整的通过HTTP的方式返回action了。通过这样的方式也可以实现webService的功能了。不过需要在Struts的配置文件里,把改action设置为不拦截,不然Action会拦截。

目录
相关文章
|
5月前
|
存储 人工智能 Java
java之通过Http下载文件
本文介绍了使用Java实现通过文件链接下载文件到本地的方法,主要涉及URL、HttpURLConnection及输入输出流的操作。
359 0
|
JSON Java Apache
非常实用的Http应用框架,杜绝Java Http 接口对接繁琐编程
UniHttp 是一个声明式的 HTTP 接口对接框架,帮助开发者快速对接第三方 HTTP 接口。通过 @HttpApi 注解定义接口,使用 @GetHttpInterface 和 @PostHttpInterface 等注解配置请求方法和参数。支持自定义代理逻辑、全局请求参数、错误处理和连接池配置,提高代码的内聚性和可读性。
629 3
|
Java Maven Windows
使用Java创建集成JACOB的HTTP服务
本文介绍了如何在Java中创建一个集成JACOB的HTTP服务,使Java应用能够调用Windows的COM组件。文章详细讲解了环境配置、动态加载JACOB DLL、创建HTTP服务器、实现IP白名单及处理HTTP请求的具体步骤,帮助读者实现Java应用与Windows系统的交互。作者拥有23年编程经验,文章来源于稀土掘金。著作权归作者所有,商业转载需授权。
337 2
使用Java创建集成JACOB的HTTP服务
|
JSON Java 数据格式
java操作http请求针对不同提交方式(application/json和application/x-www-form-urlencoded)
java操作http请求针对不同提交方式(application/json和application/x-www-form-urlencoded)
263 25
java操作http请求针对不同提交方式(application/json和application/x-www-form-urlencoded)
|
10月前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
694 7
|
JSON Java fastjson
Java Http 接口对接太繁琐?试试 UniHttp 框架吧
UniHttp 是一个声明式的 HTTP 接口对接框架,旨在简化第三方 HTTP 接口的调用过程。通过注解配置,开发者可以像调用本地方法一样发起 HTTP 请求,无需关注请求的构建和响应处理细节。框架支持多种请求方式和参数类型,提供灵活的生命周期钩子以满足复杂的对接需求,适用于企业级项目的快速开发和维护。GitHub 地址:[UniAPI](https://github.com/burukeYou/UniAPI)。
|
缓存 负载均衡 安全
|
JavaScript 安全 Java
谈谈UDP、HTTP、SSL、TLS协议在java中的实际应用
下面我将详细介绍UDP、HTTP、SSL、TLS协议及其工作原理,并提供Java代码示例(由于Deno是一个基于Node.js的运行时,Java代码无法直接在Deno中运行,但可以通过理解Java示例来类比Deno中的实现)。
271 1
|
JSON Java 数据格式
java操作http请求针对不同提交方式(application/json和application/x-www-form-urlencoded)
java操作http请求针对不同提交方式(application/json和application/x-www-form-urlencoded)
516 1
|
Java 数据处理 开发者
Java Http 接口对接太繁琐?试试 UniHttp 框架~
【10月更文挑战第10天】在企业级项目开发中,HTTP接口对接是一项常见且重要的任务。传统的编程式HTTP客户端(如HttpClient、Okhttp)虽然功能强大,但往往需要编写大量冗长且复杂的代码,这对于项目的可维护性和可读性都是一个挑战。幸运的是,UniHttp框架的出现为这一问题提供了优雅的解决方案。
367 0

热门文章

最新文章