How to obtain a Web Captcha picture and save it to a local (Java implementation) [problem points: 40 points, knot man Lanxuezaipiao]

Source: Internet
Author: User
Tags finally block

http://bbs.csdn.net/topics/390426978

public static String Readcheckimage (hashmap<string, string> requestmap) {
String url = requestmap.get ("url");
if (Requestmap.get ("params") = null) {
URL + = "?" + requestmap.get ("params");
}
InputStream in = null;
try {
System.out.println ("get>>>" + URL);

URL serverurl = new URL (URL);
HttpURLConnection conn = (httpurlconnection) serverurl.openconnection ();
Conn.setrequestmethod ("get");//"POST", "get"
Conn.addrequestproperty ("Cookie", Requestmap.get ("Cookie"));
Conn.addrequestproperty ("Accept-charset", "UTF-8;"); /GB2312,
Conn.addrequestproperty ("User-agent",
"Mozilla/5.0 (Windows; U Windows NT 5.1; ZH-CN; rv:1.9.2.8) firefox/3.6.8 ");
Conn.connect ();
Determine if the request was successful
if ((httpurlconnection) conn). Getresponsecode () = = 200) {
Get all response header fields
map<string, list<string>> map = Conn.getheaderfields ();
Iterate through all the response header fields
For (String Key:map.keySet ()) {
SYSTEM.OUT.PRINTLN (key + "--->" + map.get (key));
}
The returned cookie
String cookie = null;
if (Conn.getheaderfields (). Get ("Set-cookie")! = null) {
For (String S:conn.getheaderfields (). Get ("Set-cookie")) {
Cookie + = s;
}
}
Requestmap.put ("Cookie", Requestmap.get ("cookie") + cookie);
in = Conn.getinputstream ();

BufferedImage bi = imageio.read (in);
File F = new file ("Check_img.jpg");
Imageio.write (BI, "JPG", f);

return F.getabsolutepath ();
} else {
System.out.println ("Download code image failed!") ");
}
} catch (Exception e) {
E.printstacktrace ();
}//using the finally block to close the input stream
finally {
try {
if (in = null) {
In.close ();
}
} catch (Exception E2) {
E2.printstacktrace ();
}
}
return null;
}

How to obtain a Web Captcha picture and save it to a local (Java implementation) [problem points: 40 points, knot man Lanxuezaipiao]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.