-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.js
66 lines (61 loc) · 1.91 KB
/
url.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
importPackage(java.net);
importPackage(java.io);
importPackage(java.lang);
exports = url = {
openConnection: function(u) {
var u = new URL(u)
var connection = u.openConnection()
return connection
},
getResponse: function(conn) {
var answer = new StringBuffer();
var reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
var line;
while ((line = reader.readLine()) != null) {
answer.append(line);
}
reader.close();
return answer.toString();
},
setPost: function(connection, data) {
connection.setDoOutput(true);
connection.setRequestMethod("POST");
// write data
var writer = new OutputStreamWriter(connection.getOutputStream());
// write parameters for POST
writer.write(data);
writer.flush();
},
get: function(u) {
var conn = url.openConnection(u)
return url.getResponse(conn)
},
post: function(u, data) {
var conn = url.openConnection(u)
url.setPost(conn, data)
return url.getResponse(conn)
},
fetchUrl: function(u) {
var bytes = com.google.appengine.api.urlfetch.URLFetchServiceFactory.getURLFetchService().fetch( new URL(u) ).getContent()
return bytes
},
tests: {
getRequest: function() {
var html = url.get('http://google.com')
if(!html) throw 'error getting url'
},
getWithValidContent: function() {
var html = url.get('http://google.com')
if(html.indexOf('Google') == -1) throw 'word Google not found in google.com'
},
postRequest: function() {
var html = url.post('http://thumbtool.phpotdel.ru/')
if(!html) throw 'error posting'
},
postWithValidContent: function() {
var q = 'query=http://facebook.com&results_needed=1&op=Get it!'
var html = url.post('http://thumbtool.phpotdel.ru/', q)
if(html.indexOf('thumb-small') == -1) throw 'thumbnail not found - maybe thumbtool.phpotdel.ru is down?'
}
}
};