-
Notifications
You must be signed in to change notification settings - Fork 5
/
ApacheHttpClientUrlReader.js
48 lines (37 loc) · 1.53 KB
/
ApacheHttpClientUrlReader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
var apacheHttpClientUrlReader = function(doc) {
var BufferedReader = java.io.BufferedReader;
var InputStreamReader = java.io.InputStreamReader;
var userAgent = org.apache.http.HttpHeaders.USER_AGENT;
var HttpResponse = org.apache.http.HttpResponse;
var HttpClient = org.apache.http.client.HttpClient;
var HttpGet = org.apache.http.client.methods.HttpGet;
var HttpClientBuilder = org.apache.http.impl.client.HttpClientBuilder;
var StringBuffer = java.lang.StringBuffer;
var String = java.lang.String;
var e = java.lang.Exception;
var result = new StringBuffer();
try {
var url = new String( "http://www.google.com/search?q=httpClient");
var client = HttpClientBuilder.create().build();
var request = new HttpGet(url);
// add request header
request.addHeader("User-Agent", userAgent);
var response = client.execute(request);
logger.info("RESPONSE Code : " + response.getStatusLine().getStatusCode());
var rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
result = new StringBuffer();
var line = "";
while ((line = rd.readLine()) !== null) {
result.append(line);
}
} catch (e) {
logger.error(e);
}
logger.info(result);
return doc;
}