1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.christianschenk.fetchasgooglebot;
20
21 import org.apache.http.Header;
22 import org.apache.http.HttpEntity;
23 import org.apache.http.HttpResponse;
24 import org.apache.http.client.HttpClient;
25 import org.apache.http.client.methods.HttpGet;
26 import org.apache.http.impl.client.DefaultHttpClient;
27 import org.apache.http.params.CoreProtocolPNames;
28 import org.apache.http.util.EntityUtils;
29
30
31
32
33
34
35 public class MyFetchAsGooglebot {
36
37 private final HttpClient httpclient;
38
39 public MyFetchAsGooglebot() {
40 this.httpclient = new DefaultHttpClient();
41 }
42
43
44
45
46 public void fetchUrl(final String url) {
47 try {
48 final HttpGet httpget = new HttpGet(url);
49 httpget.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Googlebot");
50
51 final HttpResponse response = this.httpclient.execute(httpget);
52 final HttpEntity entity = response.getEntity();
53
54 if (entity == null) throw new RuntimeException("Entity is null");
55
56
57
58
59 System.out.println(response.getStatusLine().toString());
60 for (final Header header : response.getAllHeaders()) {
61 System.out.println(header.toString());
62 }
63
64 System.out.println("");
65
66
67
68
69 System.out.println(EntityUtils.toString(entity));
70 } catch (final Exception ex) {
71 throw new RuntimeException(ex);
72 }
73 }
74
75 public static void main(final String[] args) {
76 final MyFetchAsGooglebot myFaG = new MyFetchAsGooglebot();
77
78 final long start = System.currentTimeMillis();
79 myFaG.fetchUrl("http://www.google.com");
80 final long end = System.currentTimeMillis();
81
82
83 System.out.println("\nThis took " + (end - start) + " ms");
84
85 }
86 }