RC_ 2016-08-05 04:53 采纳率: 60%
浏览 1310
已采纳

求大神帮忙写一个模拟登录知乎抓取登录后html文档java代码!

这是我的代码有报错,可以帮我写一个或者帮我修改成能运行也行 用httpurlconnection或httpclient

import java.io.*;
import java.net.*;
public class FormPoster
{
String str="account=*********&password=*******";

static String a="";
public InputStream post(URL url) throws IOException
{
HttpURLConnection uc=(HttpURLConnection) url.openConnection();

uc.setDoOutput(true);
uc.setDoInput(true);
uc.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");
uc.setRequestProperty("请求","POST / HTTP/1.1");
uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
uc.setRequestProperty("Accept","text/html, application/xhtml+xml");
 uc.setRequestProperty("Accept-Language","zh-CN");  
 uc.setRequestProperty("Accept-Encoding","gzip, deflate");  
 uc.setRequestProperty("Connection","Keep-Alive");
uc.setRequestProperty("Set-Cookie", "q_c1=1dcad38e4a244cb8b611d277de4335a5|1469272988000|1469272988000; l_cap_id=ZmNhNmNjODUzMjMxNDI1Njk1MzdmMmU3OThiOTVlOGU=|1470296178|87e09bd94f9aec0ae514d1a68ee2b0cd76145de6; cap_id=M2JjNzJmYzBmMzJkNDI2NTk1NWU3YzQ4ZDYyMDhjY2E=|1470296178|92d64fc0d0c0fd09454fcf7f07b0b27ca451863f; d_c0=AGDAi1LzRQqPTkeb2yGxHD3Twy27Rm9pXJg=|1469272988; _zap=4bbe642e-7b77-4a68-9a6b-83ad90501a03; __utma=51854390.1443812525.1470275701.1470275701.1470296204.2; __utmz=51854390.1470296204.2.2.utmcsr=zhihu.com|utmccn=(referral)|utmcmd=referral|utmcct=/; login=NDZiY2NiYWNmYTAzNDM3Mzk4NmUwM2NjNWEzOWUyZmU=|1470296186|c1a52509e3e165fb562a273e777c6dffcc514058; __utmb=51854390.8.10.1470296204; __utmt=1; __utmv=51854390.000--|2=registration_date=20160722=1^3=entry_date=20160723=1; n_c=1; __utmc=51854390; a_t=2.0AHAAxB5ARAoXAAAA233KVwBwAMQeQEQKAGDAi1LzRQoXAAAAYQJVTdt9ylcAj4mrzqw5nM97J_Px2AdXkry9tQflr_vYx3dBbD2WypTZRrVqGA8cnA==; z_c0=Mi4wQUhBQXhCNUFSQW9BWU1DTFV2TkZDaGNBQUFCaEFsVk4yMzNLVndDUGlhdk9yRG1jejNzbjhfSFlCMWVTdkwyMUJ3|1470296283|0fd18b8560e36d23aedf8cab27785a74b1b753b2");
 uc.setUseCaches(false);  



uc.connect();
OutputStreamWriter out=new OutputStreamWriter(uc.getOutputStream(),"UTF-8");
out.write(str);
out.write("\r\n");
out.flush();
return uc.getInputStream();

}
public static void main(String args[]) throws IOException

{
URL url=new URL("https://www.zhihu.com/#signin");
FormPoster poster=new FormPoster();

     InputStream urlStream = poster.post(url);  
            BufferedReader bufferedReader = new BufferedReader(  

           new InputStreamReader(urlStream,"utf-8"));  

           String ss = null;  
            StringBuilder total = new StringBuilder();  
            while ((ss = bufferedReader.readLine()) != null) {  
                total.append(ss);  
            }  
            System.out.print(total);
           bufferedReader.close();       

}
}
错误信息Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.zhihu.com/#signin
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at FormPoster.post(FormPoster.java:31)
at FormPoster.main(FormPoster.java:39)

  • 写回答

3条回答 默认 最新

  • xiaoxiang-chen 2016-08-05 07:27
    关注

    你有知乎的被用账号吗,给我一个,我试一下。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题