Skip to content

Commit bf37383

Browse files
committed
HttpURLConnection.getHeaderFields() implemented
1 parent cf40622 commit bf37383

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

sources/net.sf.j2s.java.core/src/javajs/util/AjaxURLConnection.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.net.HttpURLConnection;
1010
import java.net.URL;
1111
import java.net.URLConnection;
12+
import java.util.ArrayList;
13+
import java.util.HashMap;
1214
import java.util.Hashtable;
1315
import java.util.List;
1416
import java.util.Map;
@@ -59,9 +61,50 @@ protected AjaxURLConnection(URL url) {
5961

6062
@Override
6163
public String getHeaderField(String name) {
62-
return /** @j2sNative this.info && this.info.xhr && this.info.xhr.getResponseHeader(name) || */null;
64+
try {
65+
if (getResponseCode() != -1) {
66+
return /**
67+
* @j2sNative this.info && this.info.xhr &&
68+
* this.info.xhr.getResponseHeader(name) ||
69+
*/
70+
null;
71+
}
72+
} catch (IOException e) {
73+
}
74+
return null;
6375
}
6476

77+
78+
@SuppressWarnings("unused")
79+
@Override
80+
public Map<String, List<String>> getHeaderFields() {
81+
Map<String, List<String>> map = new HashMap<>();
82+
try {
83+
if (getResponseCode() != -1) {
84+
String[] data = null;
85+
/**
86+
* @j2sNative data = this.info && this.info.xhr &&
87+
* this.info.xhr.getAllResponseHeaders(); data && (data =
88+
* data.trim().split("\n"));
89+
*/
90+
// ["content-length: 996"
91+
// , "content-type: text/plain; charset=x-user-defined"
92+
// , "last-modified: Fri, 01 May 2020 11:54:13 GMT"]
93+
if (data != null) {
94+
for (int i = 0; i < data.length; i++) {
95+
String[] parts = data[i].split(":");
96+
String key = parts[0].trim();
97+
List<String> list = map.get(key);
98+
if (list == null)
99+
map.put(key, list = new ArrayList<>());
100+
list.add(parts[1].trim());
101+
}
102+
}
103+
}
104+
} catch (IOException e) {
105+
}
106+
return map;
107+
}
65108

66109
/**
67110
*
@@ -421,7 +464,7 @@ public Object getContents() {
421464
return doAjax(false, null);
422465
}
423466

424-
@Override
467+
@Override
425468
public int getResponseCode() throws IOException {
426469
/*
427470
* Check to see if have the response code already

0 commit comments

Comments
 (0)