一个servlet接口就可以了啊:
HTTP Header 请求实例
下面的实例使用 HttpServletRequest 的 getHeaderNames() 方法读取 HTTP 头信息。该方法返回一个枚举,包含与当前的 HTTP 请求相关的头信息。
一旦我们有一个枚举,我们可以以标准方式循环枚举,使用 hasMoreElements() 方法来确定何时停止,使用 nextElement() 方法来获取每个参数的名称。
//导入必需的 java 库import java.io.IOException
import java.io.PrintWriter
import java.util.Enumeration
import javax.servlet.ServletException
import javax.servlet.annotation.WebServlet
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
@WebServlet("/DisplayHeader")
//扩展 HttpServlet 类
public class DisplayHeader extends HttpServlet {
// 处理 GET 方法请求的方法
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// 设置响应内容类型
response.setContentType("text/htmlcharset=UTF-8")
PrintWriter out = response.getWriter()
String title = "HTTP Header 请求实例 - 菜鸟教程实例"
String docType =
"<!DOCTYPE html> \n"
out.println(docType +
"<html>\n" +
"<head><meta charset=\"utf-8\"><title>" + title + "</title></head>\n"+
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<table width=\"100%\" border=\"1\" align=\"center\">\n" +
"<tr bgcolor=\"#949494\">\n" +
"<th>Header 名称</th><th>Header 值</th>\n"+
"</tr>\n")
Enumeration headerNames = request.getHeaderNames()
while(headerNames.hasMoreElements()) {
String paramName = (String)headerNames.nextElement()
out.print("<tr><td>" + paramName + "</td>\n")
String paramValue = request.getHeader(paramName)
out.println("<td> " + paramValue + "</td></tr>\n")
}
out.println("</table>\n</body></html>")
}
// 处理 POST 方法请求的方法
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response)
}
}
Service 的部份 import java.io.IOExceptionimport java.io.InputStreamimport javax.servlet.ServletException
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
public class TestServlet extends HttpServlet { @Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("AAA")
InputStream in = request.getInputStream()
byte[] b = new byte[1024]
in.read(b)
System.out.println(new String(b).trim())
in.close()
}
}
import java.io.InputStream
import java.io.OutputStream
import java.net.HttpURLConnection
import java.net.URLClient的部分public class Test {
public static void main(String[] args) throws Exception {
URL url = new URL(" http://127.0.0.1:8080/security2/ac.do")
HttpURLConnection con = (HttpURLConnection) url.openConnection()
con.setDoOutput(true)
con.setDoInput(true)
OutputStream out = con.getOutputStream()
out.write("发送".getBytes())
out.close()
InputStream in = con.getInputStream()
byte[] b = new byte[in.available()]
in.read(b)
System.out.println(new String(b))
in.close()
}
}
web.xml配置<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns=" http://java.sun.com/xml/ns/javaee"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>ac</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ac</servlet-name>
<url-pattern>/ac.do</url-pattern>
</servlet-mapping>
</web-app>
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文件
package com.atoz.http
import java.io.IOExceptionimport java.io.PrintWriter
import javax.servlet.ServletExceptionimport javax.servlet.http.HttpServletimport javax.servlet.http.HttpServletRequestimport javax.servlet.http.HttpServletResponse
import com.atoz.action.order.SendSMSActionimport 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/htmlcharset=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 = nullString 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 blocke.printStackTrace() }
打字不易,望采纳,谢谢
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)