1. Import the following two packages:
: Http://files.cnblogs.com/files/zhougaojun/httpclient_lib.zip
2. Receive the 302 status code returned by Google, and get the location parameter, as follows: Key parts of the code
Defaulthttpclient httpclient =Newdefaulthttpclient (); String Location=NULL; intResponsecode = 0; Try { FinalHttpGet request =NewHttpGet (Googleurl); Org.apache.http.params.HttpParams params=NewBasichttpparams (); Params.setparameter ("Http.protocol.handle-redirects",false);//default does not allow redirectionrequest.setparams (params); HttpResponse Response=Httpclient.execute (Request); Responsecode=response.getstatusline (). Getstatuscode (); System.out.println (Responsecode); if(responsecode==200) {log.info (Messageformat.format ("[{0}]| [Success 200]| [url:{1}]| [Status:{2}] ", Tag,googleurl,responsecode)); }Else if(responsecode==302) {Org.apache.http.Header Locationheader= Response.getfirstheader ("Location"); if(locationheader!=NULL) { location=Locationheader.getvalue (); Log.info (Messageformat.format ("[{0}]| [Success {1}]| [url:{2}]| [status:{3}]| [{location:{4}}] ", tag,responsecode,googleurl,responsecode,location)); } } } Catch(Exception e) {e.printstacktrace (); Log.error (Messageformat.format ("[{0}]| [Exception]| [url:{1}]| [status:{2}]| [Content:{3}]] ", Tag,googleurl,responsecode,e.getmessage ())); }
3.Googleservice 302 Back
HttpClient get the Custom status code 302 and get the parameter location in the header