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("+86XXXXXXXXXX ");
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());
}
}
}
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("+86XXXXXXXXXX ");
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());
}
}
}