29、OSPF配置实验之被动接口
323
2023-02-22
本文目录一览:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class DemoTest1 {
public static final String GET_URL = "mall-back/if_user/store_list?storeId=32";
// public static final String POST_URL = "mall-back/if_user/store_list";
// 妙兜测试接口
public static final String POST_URL = "mdserver/service/installLock";
/**
* 接口调用 GET
*/
public static void httpURLConectionGET() {
try {
URL url = new URL(GET_URL); // 把字符串转换为URL请求地址
HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接
connection.connect();// 连接会话
// 获取输入流
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {// 循环读取流
sb.append(line);
}
br.close();// 关闭流
connection.disconnect();// 断开连接
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
System.out.println("失败!");
}
}
/**
* 接口调用 POST
*/
public static void httpURLConnectionPOST () {
try {
URL url = new URL(POST_URL);
// 将url 以 open方法返回java测试http接口的urlConnection 连接强转为HttpURLConnection连接 (标识一个url所引用java测试http接口的远程对象连接)
HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 此时cnnection只是为一个连接对象,待连接中
// 设置连接输出流为true,默认false (post 请求是以流的方式隐式的传递参数)
connection.setDoOutput(true);
// 设置连接输入流为true
connection.setDoInput(true);
// 设置请求方式为post
connection.setRequestMethod("POST");
// post请求缓存设为false
connection.setUseCaches(false);
// 设置该HttpURLConnection实例是否自动执行重定向
connection.setInstanceFollowRedirects(true);
// 设置请求头里面的各个属性 (以下为设置内容的类型,设置为经过urlEncoded编码过的from参数)
// application/x-javascript text/xml-xml数据 application/x-javascript-json对象 application/x-www-form-urlencoded-表单数据
// ;charset=utf-8 必须要java测试http接口,不然妙兜那边会出现乱码【★★★★★】
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 建立连接 (请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行)
connection.connect();
// 创建输入输出流,用于往连接里面输出携带的参数,(输出内容为?后面的内容)
DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
String app_key = "app_key="+ URLEncoder.encode("4f7bf8c8260124e6e9c6bf094951a111", "utf-8"); // 已修改【改为错误数据java测试http接口,以免信息泄露】
String agt_num = "agt_num="+ URLEncoder.encode("10111", "utf-8"); // 已修改【改为错误数据java测试http接口,以免信息泄露】
String pid = "pid="+ URLEncoder.encode("BLZXA150401111", "utf-8"); // 已修改【改为错误数据,以免信息泄露】
String departid = "departid="+ URLEncoder.encode("10007111", "utf-8"); // 已修改【改为错误数据,以免信息泄露】
String install_lock_name = "install_lock_name="+ URLEncoder.encode("南天大门", "utf-8");
String install_address = "install_address="+ URLEncoder.encode("北京育新", "utf-8");
String install_gps = "install_gps="+ URLEncoder.encode("116.350888,40.011001", "utf-8");
String install_work = "install_work="+ URLEncoder.encode("小李", "utf-8");
String install_telete = "install_telete="+ URLEncoder.encode("13000000000", "utf-8");
String intall_comm = "intall_comm="+ URLEncoder.encode("一切正常", "utf-8");
// 格式 parm = aaa=111bbb=222ccc=333ddd=444
String parm = app_key+ agt_num+ pid+ departid+ install_lock_name+ install_address+ install_gps+ install_work+ install_telete+ intall_comm;
// 将参数输出到连接
dataout.writeBytes(parm);
// 输出完成后刷新并关闭流
dataout.flush();
dataout.close(); // 重要且易忽略步骤 (关闭流,切记!)
// System.out.println(connection.getResponseCode());
// 连接发起请求,处理服务器响应 (从连接获取到输入流并包装为bufferedReader)
BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
StringBuilder sb = new StringBuilder(); // 用来存储响应数据
// 循环读取流,若不到结尾处
while ((line = bf.readLine()) != null) {
// sb.append(bf.readLine());
sb.append(line).append(System.getProperty("line.separator"));
}
bf.close(); // 重要且易忽略步骤 (关闭流,切记!)
connection.disconnect(); // 销毁连接
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// httpURLConectionGET();
httpURLConnectionPOST();
}
}
一个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/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);
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~