时间:2021-05-19
复制代码 代码如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import java.util.StringTokenizer;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class TestPost {
public static void main(String args[]) throws IOException{
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String user_name = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
testPost(user_name , password,"d:/fileDown2.txt");
testJsoup();
}
public static void testPost(String user_name , String password,String outPath) throws IOException{
String login ="";
URL url = new URL("http://passport.mop.com");
HttpURLConnection connection = null;
connection = (HttpURLConnection) url.openConnection();//建立链接
connection.setInstanceFollowRedirects(false);
connection.setRequestProperty("Connection","keep-alive");
connection.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36");
connection.addRequestProperty("Content-Type", "application/x-/personal");
HttpURLConnection pconn = (HttpURLConnection) purl.openConnection();
pconn.setRequestProperty("Referer", str);
pconn.setRequestProperty("Cookie", myCookies.toString());
pconn.connect();
InputStream inputStream1 = pconn.getInputStream();
//BufferedReader reader1 = new BufferedReader(new InputStreamReader(inputStream1,"utf-8"));
//String line1 = reader1.readLine();
//while(line1 != null){
//System.out.println(line1);
//line1 = reader1.readLine();
//}
//reader1.close();
int chByte = 0;
FileOutputStream fileOut = new FileOutputStream(new File(outPath));
chByte = inputStream1.read();
while(chByte != -1){
fileOut.write(chByte);
chByte = inputStream1.read();
}
}
private static String getCookies(HttpURLConnection conn) {
// TODO Auto-generated method stub
// StringBuffer cookies = new StringBuffer();
StringBuilder cookies = new StringBuilder();
String headName;
for (int i = 1; (headName = conn.getHeaderField(i)) != null; i++) {
StringTokenizer st = new StringTokenizer(headName, "; ");
while (st.hasMoreTokens()) {
cookies.append(st.nextToken() + "; ");
}
}
return cookies.toString();
}
private static void testJsoup() throws IOException{
//解析html文档
File input = new File("D:/fileDown2.txt");
Document doc = Jsoup.parse(input, "UTF-8");
// for(Element ele : doc.getElementsByClass("zhnc").select("ul")){
// if(!ele.select("li").toString().equals("")){
// String text = ele.select("li").text();
// System.out.println("user_name is:"+text);
// }
// }
Elements ele = doc.getElementsByClass("zhnc").select("ul");
if(!ele.select("li").toString().equals("")){
String text = ele.select("li").text();
System.out.println("user_name is:"+text);
}else{
System.out.println("登录失败");
}
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
java模拟http发送请求,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求,实例代码:packa
java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请
本文实例讲述了java通过模拟post方式提交表单实现图片上传功能。分享给大家供大家参考,具体如下:模拟表单html如下:java代码如下:packagecom
原理Android客户端模拟一个HTTP的Post请求到服务器端,服务器端接收相应的Post请求后,返回响应信息给给客户端。背景网上很多上传到java服务器上的
1.打开赶集网登录界面,先模拟登录并抓包,获得post请求的request参数2.我们只需构造出上面的参数传入formdata即可参数分析: setcooki