多平台统一管理软件接口,如何实现多平台统一管理软件接口
275
2023-03-19
本文目录一览:
一个servlet接口就可以http接口开发实例了啊http接口开发实例:
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/html;charset=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" +
"<thHeader 名称</th<thHeader 值</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);
}
}
private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
/// <summary
/// 创建POST方式的HTTP请求
/// </summary
/// <param name="url"请求的URL</param
/// <param name="parameters"随同请求POST的参数名称及参数值字典</param
/// <param name="timeout"请求的超时时间</param
/// <returns</returns
public static string PostHttpResponse(string url, Dictionary<string, string parameters,
int? timeout)
{
try
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ServicePoint.Expect100Continue = false;
request.ServicePoint.UseNagleAlgorithm = false; //是否使用 Nagle 不使用 提高效率
//request.AllowWriteStreamBuffering = false; //数据是否缓冲 false 提高效率
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = DefaultUserAgent;
if (timeout.HasValue)
{
request.Timeout = timeout.Value;
}
else
{
request.Timeout = 20000;
}
Encoding requestEncoding = Encoding.GetEncoding("UTF-8");
//如果需要POST数据
if (!(parameters == null || parameters.Count == 0))
{
StringBuilder buffer = new StringBuilder();
int i = 0;
foreach (string key in parameters.Keys)
{
if (i 0)
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
}
i++;
}
byte[] data = requestEncoding.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
//获取响应,并设置响应编码
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding == null || encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
//读取响应流
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string returnData = reader.ReadToEnd();
reader.Dispose();
response.Close();
return returnData;
}
catch (Exception ex)
{
MessageUtil.ShowWarning(ex.Message);
}
return "";
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~