java模拟登录新版知乎

package phamos;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Date;
public class Te {


/**
* @param args
* @throws Exception 
*/
public static void main(String[] args) throws Exception {
String a=getCookie();




Login(a);


}









public static String getCookie() throws IOException{

String url="https://www.zhihu.com/api/v3/oauth/captcha?lang=en";
URL urlx = new URL(url);
HttpURLConnection uc = (HttpURLConnection) urlx.openConnection();
uc.setDoInput(true);
uc.setDoOutput(true);
uc.setUseCaches(false);
uc.setRequestMethod("GET");
uc.setInstanceFollowRedirects(true);
uc.setConnectTimeout(10 * 1000); // 10缂佸甯熺换娑㈠箳閵夈劎孝闁跨噦鎷�
uc.setReadTimeout(10*1000);
// //30缂佸甯熼浼村矗閺嶎剛孝闁跨噦鎷�
// uc.setRequestProperty("Cache-Control",
// "no-cache, must-revalidate");
uc.setRequestProperty("Connection", "Keep-Alive");
uc.setRequestProperty(
"Accept",
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-shockwave-flash, */*");
uc.setRequestProperty("Accept-Language", "zh-cn");


uc.setRequestProperty("Content-Type","multipart/form-data; boundary=----WebKitFormBoundary2Tg5oWg4CHK9v2GR");

uc.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36");
uc.setRequestProperty("referer","https://www.zhihu.com/signup?next=%2F");
String cookie="_xsrf=0b522076-943a-4623-8ab4-b2f1227bc67b; d_c0=\"AABkPrXmqg2PTt7YNg_wbxot3VarPur8oaw=|1527587284\"; q_c1=6da7af5b71654fbd81a3145d00d75e68|1527587285000|1527587285000; _zap=726a3754-10f9-4806-b365-c0fd915f0cff; anc_cap_id=a940fd7719384947b5ce7a9f1510266b; tgw_l7_route=e0a07617c1a38385364125951b19eef8; capsion_ticket=\"2|1:0|10:1527590372|14:capsion_ticket|44:ZmIxMzM0ZmY5YzEwNDAzZGJjMTY4YWNkNGM4ZGE3ODY=|75839b2a6e03bbfc149cbc024f500a08cfff64fa2f1ac0beed0267cbe416fa5d\"";
uc.setRequestProperty("Cookie", cookie);
uc.setRequestProperty("authorization","oauth c3cef7c66a1843f8b3a9e6a1e3160e20");
uc.setRequestProperty("referer", "https://www.zhihu.com/signup?next=%2F");

uc.connect();

System.out.println(uc.getHeaderField("set-cookie").toString());


return uc.getHeaderField("set-cookie");



}







public static void Login(String cookie) throws Exception{

String mimeBoundary="----WebKitFormBoundary2Tg5oWg4CHK9v2GR";

String httpurl="http://postimages.org/json/rr";
httpurl="https://www.zhihu.com/api/v3/oauth/sign_in";

URL urlx = new URL(httpurl);
HttpURLConnection uc = (HttpURLConnection) urlx.openConnection();
uc.setDoInput(true);
uc.setDoOutput(true);
uc.setUseCaches(false);
uc.setRequestMethod("POST");
uc.setInstanceFollowRedirects(true);
uc.setConnectTimeout(10 * 1000); // 10缂佸甯熺换娑㈠箳閵夈劎孝闁跨噦鎷�
uc.setReadTimeout(10*1000);
// //30缂佸甯熼浼村矗閺嶎剛孝闁跨噦鎷�
// uc.setRequestProperty("Cache-Control",
// "no-cache, must-revalidate");
uc.setRequestProperty("Connection", "Keep-Alive");
uc.setRequestProperty(
"Accept",
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-shockwave-flash, */*");
uc.setRequestProperty("Accept-Language", "zh-cn");


uc.setRequestProperty("Content-Type","multipart/form-data; boundary=----WebKitFormBoundary2Tg5oWg4CHK9v2GR");


uc.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36");

uc.setRequestProperty("authorization","oauth c3cef7c66a1843f8b3a9e6a1e3160e20");













System.out.println(cookie);
uc.addRequestProperty("Cookie",cookie);

uc.connect();



OutputStream output = uc.getOutputStream();

long s =new Date().getTime();

System.out.println(s);

StringBuffer sb = new StringBuffer();

sb = sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"client_id\"");

sb.append("\r\n\r\n");
sb.append("c3cef7c66a1843f8b3a9e6a1e3160e20");
sb.append("\r\n");
//第二个提交的参数

//提交的第三个参数

sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"grant_type\"");
sb.append("\r\n\r\n");
sb.append("password");
sb.append("\r\n");



sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"timestamp\"");
sb.append("\r\n\r\n");
sb.append(s);
sb.append("\r\n");


sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"source\"");
sb.append("\r\n\r\n");
sb.append("com.zhihu.web");
sb.append("\r\n");


sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"signature\"");
sb.append("\r\n\r\n");
sb.append(HMACSHA1.getSignature("password"+"c3cef7c66a1843f8b3a9e6a1e3160e20"+"com.zhihu.web"+s, "d1b964811afb40118a12068ff74a12f4"));
sb.append("\r\n");

sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"username\"");
sb.append("\r\n\r\n");
sb.append("+86
XXXXXXXXXX ");
sb.append("\r\n");

sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"password\"");
sb.append("\r\n\r\n");
sb.append("PPPPPPPPPPPPPP");
sb.append("\r\n");

sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"captcha\"");
sb.append("\r\n\r\n");
sb.append("");
sb.append("\r\n");


//第五个参数


sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"lang\"");
sb.append("\r\n\r\n");
sb.append("cn");
sb.append("\r\n");


sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"ref_source\"");
sb.append("\r\n\r\n");
sb.append("homepage");
sb.append("\r\n");
//




sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"utm_source\"");
sb.append("\r\n\r\n");
sb.append("");
sb.append("\r\n");




sb.append("--" + mimeBoundary + "--\r\n");
System.out.println(sb.toString());
output.write(sb.toString().getBytes()); 


  output.flush();  
  output.close();  

System.out.println(uc.getResponseCode());

String responseMessage = ""; 
StringBuffer response = new StringBuffer();
BufferedReader reader = null; 
reader = new BufferedReader(new InputStreamReader(uc.getInputStream(),"UTF-8")); 
 
         while ((responseMessage = reader.readLine()) != null) { 
             response.append(responseMessage); 
             response.append("\n"); 
         } 


     
     
         System.out.println(response.toString()); 
         





}




}


}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值