1.導入如下兩個包:
下載地址:http://files.cnblogs.com/files/zhougaojun/httpclient_lib.zip
2.接收Google返回的302狀態碼,並獲得Location參數,如下是代碼關鍵部分
DefaultHttpClient httpclient = new DefaultHttpClient(); String location = null; int responseCode = 0; try { final HttpGet request = new HttpGet(googleUrl); org.apache.http.params.HttpParams params = new BasicHttpParams(); params.setParameter("http.protocol.handle-redirects", false); // 默認不讓重定向 request.setParams(params); HttpResponse response = httpclient.execute(request); responseCode = response.getStatusLine().getStatusCode(); System.out.println(responseCode); if(responseCode==200){ LOG.info(MessageFormat.format("[{0}]|[成功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}]|[成功{1}]|[url:{2}]|[status:{3}]|[{location:{4}}]",tag,responseCode,googleUrl,responseCode,location)); } } } catch (Exception e) { e.printStackTrace(); LOG.error(MessageFormat.format("[{0}]|[異常]|[url:{1}]|[status:{2}]|[content:{3}]]",tag,googleUrl,responseCode,e.getMessage())); }
3.Googleservice 302返回