java代码怎么实现取到网页返回的响应参数? java怎么做网页
如何用java通过post的方式实现对一个网页接口传递参数并获取返回的值
方法1:使用HttpURLConnection
导入包:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.HttpURLConnection;
import java.ProtocolException;
import java.SocketTimeoutException;
import java.URL;主要代码(耗时操作一般另起线程,并处理异常):
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("");//这里放你的请求地址
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST"); //设置请求头,设置请求方式为POST
connection.setDoOutput(true);//允许写出
connection.setDoInput(true);//允许读入
connection.setUseCaches(false);//不使用缓存
connection.connect();//连接
//传递数据
String body = "userName=zhangsan&password=123456";//传递参数
//String body = "{userName:zhangsan,password:123456}";//传递json数据
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
writer.write(body);
writer.close();
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder result = new StringBuilder();//存储返回的数据
String line;
while((line = reader.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
} catch (SocketTimeoutException timeExcep) {
//"请求服务器超时";
}catch (ProtocolException pe) {
//"获取资讯超时";
} catch (IOException e) {
//"服务器响应失败";
}
}
}).start();方法2:使用Jsoup(这个是第三方jar包,使用很方便)
导入所需包(Jsoup包自行查询下载):
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import .jsoup.Connection;
import .jsoup.Jsoup;例子(Post登陆):
public void login(String login_url, String username, String password) {
//传递数据
Map<String, String> dataMap = new HashMap<>();
dataMap.put("username", username);
dataMap.put("password", password);
//设置请求头(可选)
Map headerMap = new HashMap();
headerMap.put("Referer", login_url);
try {
Connection connLogin = Jsoup.connect(login_url);
connLogin.headers(headerMap);
//请求并返回响应体
Connection.Response response = connLogin
.method(Connection.Method.POST)//设置请求方法
.headers(headerMap)//设置请求头
.data(dataMap)//设置请求数据
.execute();//执行
//返回数据
System.out.println(response.parse().text());
} catch (IOException e) {
e.printStackTrace();
}
}
Java请求一个URL。获取网站返回的数据。
public static String SendGET(String url,String param){
String result="";//访问返回结果
BufferedReader read=null;//读取访问结果
try {
//创建url
URL realurl=new URL(url+"?"+param);
//打开连接
URLConnection connection=realurl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//建立连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段,获取到cookies等
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
read = new BufferedReader(new InputStreamReader(
connection.getInputStream(),"UTF-8"));
String line;//循环读取
while ((line = read.readLine()) != null) {
result += line;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(read!=null){//关闭流
try {
read.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}