简单的用一个例子;
1搭建个简单的服务器
eclipse新建一个web项目 新建一个jsp文件
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%System.out.print("请求...."); %>
头:<%=request.getMethod() %>
姓名:<%=request.getParameter("name") %>
年龄:<%=request.getParameter("age") %>
</body>
</html>
就是这么简单
2.客户端 先看看 HttpURLConnection get请求
final ProgressDialog progressDialog = ProgressDialog.show(this, null, "正在请求中....");
new Thread() {
@Override
public void run() {
super.run();
String path = "http://192.168.1.5:8080/20190313/NewFile.jsp" + "?name=wf&age=14";
try {
URL url = new URL(path); //包含请求路径的构造方法
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //得到连接对象
connection.setRequestMethod("GET"); //请求方式
connection.setConnectTimeout(5000); //设置连接超时
connection.setReadTimeout(5000); //读取超时
connection.connect(); //连接服务器
int response = connection.getResponseCode(); //服务器返回的结果码
if (response == 200) {
InputStream is = connection.getInputStream();
byte[] bytes = new byte[1024];
int len = -1;
//将得到的io流转换成字符串
ByteArrayOutputStream bs = new ByteArrayOutputStream();
while ((len = is.read(bytes)) != -1) {
bs.write(bytes, 0, len);
}
final String reult = bs.toString();
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(reult);
progressDialog.dismiss();
}
});
is.close(); //关闭流 (偷懒)
bs.close();
}
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
progressDialog.dismiss();
}
}
}.start();
get请求就是这么简单