访问的地址是:http://aq.qq.com/cn2/login_limit/login_limit_index
public final static String CONTENT_TYPE = "Content-Type";
public static void main(String[] args) throws Exception {
// login
// 验证码的位置
// Content content = getRandom("GET",
// "http://localhost:8080/back/random.action", null, null, false,"d:/");
Content content = getRandom(
"GET",
"http://captcha.qq.com/getimage?aid=2001601&0.6973676234483719",
null, null, false, "d:/");
// build request headers & do rate of user review
List<String> lsit = content.getHeaders().get("Set-Cookie");
Map<String, String> resmap = new HashMap<String, String>();
String cookie = "";
if (lsit != null) {
StringBuffer sb = new StringBuffer();
boolean isLast = false;
int i = 0;
for (String val : lsit) {
i++;
if (i == lsit.size()) {
isLast = true;
}
int pos = val.indexOf("=");
if (pos != -1) {
String cookieName = val.substring(0, pos);
String cookieVal = val.substring(pos + 1);
System.out.println(cookieName + ":" + cookieVal);
cookieVal = cookieVal.split(";")[0];
if (isLast) {
sb.append(cookieName + "=" + cookieVal);
} else {
sb.append(cookieName + "=" + cookieVal + ";");
}
}
}
cookie = sb.toString();
}
String a = "";
System.out.print("请输入验证码:");
BufferedReader strin = new BufferedReader(new InputStreamReader(
System.in));
try {
a = strin.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("输入的数是:" + a);
String userCode = "admin";
String account = "1158752036";
// String loginUrl = "http://localhost:9090/w/login.action";
String rateReviewUrl = "http://aq.qq.com/cn2/ajax/check_verifycode?verify_code="
+ URLEncoder.encode(a, "utf-8")
+ "&account="
+ URLEncoder.encode(account, "utf-8")
+ "&session_type="
+ URLEncoder.encode("on_rand", "utf-8");
// String rateReviewUrl =
// "http://fdjkpoi.xicp.net/udp/baseinfo/getQQ.action";
Map<String, String> paramMap = new HashMap<String, String>();
// content = curl("POST", loginUrl, paramMap, resmap, false, "");
System.out
.println("第一次 content.getBody()= " + content == null ? "no body"
: content.getBody());
// build request headers & do rate of user review
// paramMap = new HashMap<String, String>();
content = curl("get", rateReviewUrl, paramMap, cookie, false, "");
// inFile(content.getBody(), "D:/sss.txt");
}
public static Content curl(String method, // 方法类型
String sUrl,// 要解析的URL
Map<String, String> paramMap, // 存放用户名和密码的map
String requestHeaderMap,// 存放COOKIE的map
boolean isOnlyReturnHeader, String path) throws Exception {// 存放文件路径
System.out.println("-------------" + sUrl + "-------------------");
Content content = null;
try {
URL getUrl = new URL(sUrl);
// 根据拼凑的URL,打开连接,URL.openConnection函数会根据URL的类型,
// 返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) getUrl
.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept",
"application/json, text/javascript, */*");
connection.setRequestProperty("Accept-Encoding", "UTF-8");
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
connection.setRequestProperty("Cookie", requestHeaderMap);
// 进行连接,但是实际上get request要在下一句的connection.getInputStream()函数中才会真正发到
// 服务器
connection.connect();
// 取得输入流,并使用Reader读取
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(),"UTF-8"));
String lines;
while ((lines = reader.readLine()) != null) {
System.out.println(lines);
}
reader.close();
// 断开连接
connection.disconnect();
} catch (Exception e) {
return null;
} finally {
}
return content;
}
public static Content getRandom(String method, String sUrl,// 要解析的url
Map<String, String> paramMap, // 存放用户名和密码的map
Map<String, String> requestHeaderMap,// 存放COOKIE的map
boolean isOnlyReturnHeader, String path) {
Content content = null;
HttpURLConnection httpUrlConnection = null;
InputStream in = null;
try {
URL url = new URL(sUrl);
boolean isPost = "POST".equals(method);
if (method == null
|| (!"GET".equalsIgnoreCase(method) && !"POST"
.equalsIgnoreCase(method))) {
method = "POST";
}
URL resolvedURL = url;
URLConnection urlConnection = resolvedURL.openConnection();
httpUrlConnection = (HttpURLConnection) urlConnection;
httpUrlConnection.setRequestMethod(method);
httpUrlConnection.setRequestProperty("Accept-Language",
"zh-cn,zh;q=0.5");
// Do not follow redirects, We will handle redirects ourself
httpUrlConnection.setInstanceFollowRedirects(false);
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setDoInput(true);
httpUrlConnection.setConnectTimeout(5000);
httpUrlConnection.setReadTimeout(5000);
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setDefaultUseCaches(false);
httpUrlConnection.connect();
int responseCode = httpUrlConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK
|| responseCode == HttpURLConnection.HTTP_CREATED) {
byte[] bytes = new byte[0];
if (!isOnlyReturnHeader) {
DataInputStream ins = new DataInputStream(httpUrlConnection
.getInputStream());
// 验证码的位置
DataOutputStream out = new DataOutputStream(
new FileOutputStream(path + "/code.bmp"));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = ins.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
out.close();
ins.close();
}
String encoding = null;
if (encoding == null) {
encoding = getEncodingFromContentType(httpUrlConnection
.getHeaderField(CONTENT_TYPE));
}
content = new Content(sUrl, new String(bytes, encoding),
httpUrlConnection.getHeaderFields());
}
} catch (Exception e) {
return null;
} finally {
if (httpUrlConnection != null) {
httpUrlConnection.disconnect();
}
}
return content;
}
public static String getEncodingFromContentType(String contentType) {
String encoding = null;
if (contentType == null) {
return null;
}
StringTokenizer tok = new StringTokenizer(contentType, ";");
if (tok.hasMoreTokens()) {
tok.nextToken();
while (tok.hasMoreTokens()) {
String assignment = tok.nextToken().trim();
int eqIdx = assignment.indexOf('=');
if (eqIdx != -1) {
String varName = assignment.substring(0, eqIdx).trim();
if ("charset".equalsIgnoreCase(varName)) {
String varValue = assignment.substring(eqIdx + 1)
.trim();
if (varValue.startsWith("\"")
&& varValue.endsWith("\"")) {
// substring works on indices
varValue = varValue.substring(1,
varValue.length() - 1);
}
if (Charset.isSupported(varValue)) {
encoding = varValue;
}
}
}
}
}
if (encoding == null) {
return "UTF-8";
}
return encoding;
}
// 这个是输出
public static boolean inFile(String content, String path) {
PrintWriter out = null;
File file = new File(path);
try {
if (!file.exists()) {
file.createNewFile();
}
out = new PrintWriter(new FileWriter(file));
out.write(content);
out.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
out.close();
}
return false;
}
public static String getHtmlReadLine(String httpurl) {
String CurrentLine = "";
String TotalString = "";
InputStream urlStream;
String content = "";
try {
URL url = new URL(httpurl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.connect();
System.out.println(connection.getResponseCode());
urlStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(urlStream, "utf-8"));
while ((CurrentLine = reader.readLine()) != null) {
TotalString += CurrentLine + "\n";
}
content = TotalString;
} catch (Exception e) {
}
return content;
}
}
class Content {
private String url;
private String body;
private Map> m_mHeaders = new HashMap>();
public Content(String url, String body, Map<String, List<String>> headers) {
this.url = url;
this.body = body;
this.m_mHeaders = headers;
}
public String getUrl() {
return url;
}
public String getBody() {
return body;
}
public Map<String, List<String>> getHeaders() {
return m_mHeaders;
}
贴上代码, 1158752036 QQ号已经被冻结,但是返回是 {"Err":"0" }
我输入没有冻结的QQ号 返回的也一样,输入错误的验证码,返回是 {"Err":"您输入的验证码有误" },证明已经通了,不知是否这个地址 还有其他的参数吗? 小白一个往大神教导