当前位置: 首页 > php>阅读正文

php充当http网络接口调试器

2021.9.3 朱丰华 1071 次 留下评论 2858字

http协议,是目前广泛使用的网络传输协议

这里使用php返回客户端发送的数据,充当接口调试器,http请求的组成

请求方式 uri 协议/版本
请求头...
请求头...

请求体

请求头和请求体可以有也可以没有,通常来说有多个请求头

请求头和请求体,必须有一个空行。

在使用http 1.1版本以上(http2.0已投入使用),是长链接,发送body时应该在header中指出body长度。

php充当服务器

按照请求协议,返回http请求代码。

<?php

$str = "";
$str.= "*******您正在使用52的小窝接口在线调试*******\r\n";
$str.= $_SERVER['REQUEST_METHOD'].' '.$_SERVER['REQUEST_URI'].' '.$_SERVER['SERVER_PROTOCOL']."\r\n";
$str.= "------正在返回header参数:\r\n";
foreach (getallheaders() as $name => $value) {
    $str.= "$name: $value\r\n";
}
$str.= "------正在返回body参数:\r\n";
$str.= "\r\n";  // 空行

$str.= file_get_contents('php://input');

echo $str;  // 直接返回数据

$handle = fopen("data.txt","w");  // 同时记录到data.txt中,以便查阅
fwrite($handle,$str);
fclose($handle);

java充当客户端

这里使用 javax.net.ssl 包下的 HttpsURLConnection 类完成此功能

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;

public class Human { // 测试类


/**
* HttpsURLConnection上传文件流
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//本地图片,这里使用文本文件代替
java.io.File file = new java.io.File("C:/a.txt");
FileInputStream fileInputStream = new FileInputStream(file);
//对接外部接口
String urlString = "https://www.52dixiaowo.com/tools/index.php?username=abc";

URL url = new URL(urlString);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
// 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在
// http正文内,因此需要设为true, 默认情况下是false;
con.setDoOutput(true);
// 设置是否从httpUrlConnection读入,默认情况下是true;
con.setDoInput(true);
// 设定请求的方法为"POST",默认是GET
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent","pan.baidu.com");
// Post 请求不能使用缓存
con.setUseCaches(false);
// 设定传送的内容类型是可序列化的java对象
// (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)
// con.setRequestProperty("Content-type", "application/x-java-serialized-object");
OutputStream out = con.getOutputStream();

//读取本地图片文件流
FileInputStream inputStream = new FileInputStream(file);
byte[] data = new byte[2048];
int len = 0;
int sum = 0;
while ((len = inputStream.read(data)) != -1) {
//将读取到的本地文件流读取到HttpsURLConnection,进行上传
out.write(data, 0, len);
sum = len + sum;
}

System.out.println("上传图片大小为:" + sum);

out.flush();
inputStream.close();
out.close();

int code = con.getResponseCode(); //获取post请求返回状态
System.out.println("code=" + code + " url=" + url);
if (code == 200) {
InputStream inputStream2 = con.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream2.read(data)) != -1) {
bos.write(data, 0, len);
}
inputStream2.close();
String content = bos.toString();
bos.close();
System.out.println(content); // 返回内容

}
//断开HttpsURLConnection连接
con.disconnect();
}
}

程序执行结果:

上传图片大小为:16
code=200 url=https://www.52dixiaowo.com/tools/index.php?username=abc
*******您正在使用52的小窝接口在线调试*******
POST /tools/index.php?username=abc HTTP/1.1
------正在返回header参数:
Content-Length: 16
Content-Type: application/x-www-form-urlencoded
Connection: keep-alive
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Host: www.52dixiaowo.com
Pragma: no-cache
Cache-Control: no-cache
User-Agent: pan.baidu.com
------正在返回body参数:

abcfsdfdsfdsfdsf

Process finished with exit code 0

本篇完,还有疑问?留下评论吧

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注