Skip to content

Commit 5d1d635

Browse files
author
zhourenjian
committed
Fix bug of HttpRequest for "Set-Cookie" header, enlarge reading buffer
1 parent 55f11ae commit 5d1d635

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

sources/net.sf.j2s.ajax/ajaxcore/net/sf/j2s/ajax/HttpRequest.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.net.URL;
2323
import java.util.HashMap;
2424
import java.util.Iterator;
25+
import java.util.List;
2526
import java.util.Map;
2627
import java.util.regex.Matcher;
2728
import java.util.regex.Pattern;
@@ -281,7 +282,27 @@ public String getAllResponseHeaders() {
281282
* @return String the response header value.
282283
*/
283284
public String getResponseHeader(String key) {
284-
return connection.getHeaderField(key);
285+
Map<String, List<String>> headerFields = connection.getHeaderFields();
286+
List<String> list = headerFields.get(key);
287+
if (list == null) {
288+
return null;
289+
}
290+
if (list.size() == 0) {
291+
return "";
292+
}
293+
String headerValue = null;
294+
for (Iterator<String> itr = list.iterator(); itr.hasNext();) {
295+
String value = (String) itr.next();
296+
if (value != null) {
297+
if (headerValue == null) {
298+
headerValue = value;
299+
} else {
300+
headerValue = value + "\r\n" + headerValue;
301+
}
302+
}
303+
}
304+
return headerValue; // may have multiple Set-Cookie headers
305+
// return connection.getHeaderField(key);
285306
}
286307
/**
287308
* Open connection for HTTP request with given method and URL
@@ -407,10 +428,12 @@ private void request() {
407428
} else {
408429
connection.setReadTimeout(30000); // 30s
409430
}
431+
connection.setInstanceFollowRedirects(false);
410432
connection.setDoInput(true);
411433
connection.setRequestMethod(method);
412434
connection.setRequestProperty("User-Agent",
413-
"Mozilla/5.0 (compatible; Java2Script/2.0.0)");
435+
// "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 GTB5 (Java2Script/2.0.0)");
436+
"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 GTB5");
414437
if ("post".equalsIgnoreCase(method)) {
415438
connection.setDoOutput(true);
416439
connection.setRequestProperty("Content-Type",
@@ -471,8 +494,8 @@ private void request() {
471494

472495
receiving = initializeReceivingMonitor();
473496

474-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
475-
byte[] buffer = new byte[1024];
497+
ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);
498+
byte[] buffer = new byte[10240];
476499
int read;
477500
while (!toAbort && (read = is.read(buffer)) != -1) {
478501
if (checkAbort()) return; // stop receiving anything

sources/net.sf.j2s.ajax/j2sajax/HttpRequest.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ c$.prototype.open = function (method, url, async, user) {
7070
*/
7171
c$.prototype.open = function (method, url, async, user, password) {
7272
this.transport.open (method, url, async, user, password);
73-
try {
74-
if (ClassLoader != null && ClassLoader.isGecko) {
75-
this.transport.setRequestHeader ("User-Agent", "Java2Script/2.0.0");
76-
}
77-
} catch (e) {
78-
}
73+
//try {
74+
// if (ClassLoader != null && ClassLoader.isGecko) {
75+
// this.transport.setRequestHeader ("User-Agent", "Java2Script/2.0.0");
76+
// }
77+
//} catch (e) {
78+
//}
7979
try {
8080
var l = window.location;
8181
this.transport.setRequestHeader ("Referer",

0 commit comments

Comments
 (0)