Leser: 2
|< 1 2 3 4 5 >| | 45 Einträge, 5 Seiten |
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//****************************************************************************
// Licence:
// 1. Keep this copyright notice.
// 2. Keep attribution to any authors listed above, including yourself
// 3. Use it for free.
//****************************************************************************
// Log of changes:
//
// 0.5 - 16 Mar 2005: HttpResponse Headers improved
// 0.4 - 15 Mar 2005: HttpRequest and OpenEx added by esskar
// 0.3 - 15 Mar 2005: Header usage fixed by esskar
// 0.2 - 15 Mar 2005: Copyright included by esskar
// 0.1 - 14 Mar 2005: Project started by Sascha Kiefer (esskar)
//
//****************************************************************************
var HTTPERROR_SUCCESS = 0;
var HTTPERROR_INVALIDOBJECT = 1;
var HTTPERROR_CALLBACK = 2;
var HTTPERROR_OPEN = 3;
function Http(Interface) {
//Properties
this.Version = '0.5';
this.IsAsync = false;
this.Interface = Interface;
this.LastException = '';
if(this.Interface == null) {
if(typeof XMLHttpRequest != 'undefined') {
/* try this first; maybe MS
will implement it someday :) */
this.Interface = new XMLHttpRequest();
}
if(this.Interface == null) {
var axos = new Array( // newest on top
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP',
'Microsoft.XMLHTTP'
);
for(var i = 0; this.Interface == null && i < axos.length; i++ ) {
try {
this.Interface = new ActiveXObject(axos[i]);
} catch(e) {
this.LastException = e;
this.Interface = null;
}
}
}
}
//Methods
this.IsValid = CallHttpIsValid;
this.Get = CallHttpGet;
this.Post = CallHttpPost;
this.Open = CallHttpOpen;
this.OpenEx = CallHttpOpenEx;
this.SetHeader = CallHttpSetHeader;
this.GetResponse = CallHttpGetResponse;
}
function CallHttpIsValid() {
return this.Interface != null;
}
function HttpRequest() {
this.Method = 'GET';
this.Url = '';
this.Headers = new Array();
this.Body = null;
this.Callback = null;
}
function CallHttpOpenEx(req) {
return this.Open(
req.Method,
req.Url,
req.Body,
req.Callback,
req.Headers
);
}
function CallHttpOpen(method, url, data, callback, headers) {
if(this.IsValid()) {
if(!method) method = 'GET';
if(!data) data = null;
if(this.IsAsync) {
if(typeof callback != 'function')
return HTTPERROR_CALLBACK;
this.Interface.onreadystatechange = callback;
}
try {
this.Interface.open(method, url, this.IsAsync);
} catch(e) { // probably: 'access is denied'
this.LastException = e;
return HTTPERROR_OPEN;
}
// Send userdefined headers
if(headers != null) {
for(var header in headers) {
this.SetHeader(header, headers[header]);
}
}
this.Interface.send(data);
return HTTPERROR_SUCCESS;
}
return HTTPERROR_INVALIDOBJECT;
}
function CallHttpGet(url, callback, headers) {
return this.Open('GET', url, null, callback, headers);
}
function CallHttpPost(url, data, callback, headers) {
return this.Open('POST', url, data, callback, headers);
}
function CallHttpSetHeader(headername, headervalue) {
if(!this.IsValid())
return false;
var retval = true;
try {
this.Interface.setRequestHeader(headername, headervalue);
} catch(e) {
retval = false;
this.LastException = e;
}
return retval;
}
function HttpResponse() {
this.Status = 0;
this.StatusText = '';
this.Headers = new Array;
this.Body = '';
this.Text = '';
this.Xml = '';
this.SetHeaderString = CallHttpResponseSetHeaderString;
}
function CallHttpResponseSetHeaderString(string) {
if(!string) string = '';
var lines = string.split("\n");
for(var i = 0; i < lines.length; i++) {
var header = lines[i].split(": ");
if(header.length >= 2) {
var headername = header.shift();
var headervalue = header.join(": ");
this.Headers[headername] = headervalue;
}
}
}
function CallHttpGetResponse() {
if(this.Interface.readyState != 4)
return null;
var res = new HttpResponse();
res.Status = this.Interface.status;
res.StatusText = typeof this.Interface.statusText == 'undefined'
? ''
: this.Interface.statusText;
res.Body = typeof this.Interface.responseBody == 'undefined'
? ''
: this.Interface.responseBody;
res.Text = typeof this.Interface.responseText == 'undefined'
? ''
: this.Interface.responseText;
res.Xml = this.Interface.responseXML == null
? ''
: this.Interface.responseXML.xml;
res.SetHeaderString(this.Interface.getAllResponseHeaders());
return res;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var http = new Http();
if(http.Get('Css/default.css') == HTTPERROR_SUCCESS) {
var res = http.GetResponse();
if(res != null) {
alert('Status: ' + res.Status);
alert('StatusText: ' + res.StatusText);
for(var headername in res.Headers) {
alert(headername + " - " + res.Headers[headername]);
}
alert('Body: ' + res.Body);
alert('Text: ' + res.Text);
alert('Xml: ' + res.Xml);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var request = new HttpRequest();
request.Url = 'Css/default.css';
request.Headers['hutzel'] = 'puztel';
request.Headers['User-Agent'] = 'MyCustomUserAgent';
var http = new Http();
if(http.OpenEx(request) == HTTPERROR_SUCCESS) {
var res = http.GetResponse();
if(res != null) {
alert('Status: ' + res.Status);
alert('StatusText: ' + res.StatusText);
for(var headername in res.Headers) {
alert(headername + " - " + res.Headers[headername]);
}
alert('Body: ' + res.Body);
alert('Text: ' + res.Text);
alert('Xml: ' + res.Xml);
}
}
|< 1 2 3 4 5 >| | 45 Einträge, 5 Seiten |