Skip to content

Commit 2c1bed4

Browse files
hansonrhansonr
authored andcommitted
AjaxURLConnection should implement HttpURLConnection
1 parent 16c8d3e commit 2c1bed4

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.BufferedInputStream;
44
import java.io.IOException;
55
import java.io.InputStream;
6+
import java.net.HttpURLConnection;
67
import java.net.URL;
78
import java.net.URLConnection;
89

@@ -13,7 +14,7 @@
1314
* A method to allow a JavaScript Ajax
1415
*
1516
*/
16-
public class AjaxURLConnection extends URLConnection {
17+
public class AjaxURLConnection extends HttpURLConnection {
1718

1819
protected AjaxURLConnection(URL url) {
1920
super(url);
@@ -107,4 +108,47 @@ public Object getContents() {
107108
return doAjax(false);
108109
}
109110

111+
@Override
112+
public int getResponseCode() throws IOException {
113+
/*
114+
* We're got the response code already
115+
*/
116+
if (responseCode != -1) {
117+
return responseCode;
118+
}
119+
120+
/*
121+
* Ensure that we have connected to the server. Record
122+
* exception as we need to re-throw it if there isn't
123+
* a status line.
124+
*/
125+
Exception exc = null;
126+
try {
127+
BufferedInputStream is = (BufferedInputStream) getInputStream();
128+
if (is.available() > 40)
129+
return responseCode = HTTP_OK;
130+
is.mark(15);
131+
byte[] bytes = new byte[13];
132+
is.read(bytes);
133+
is.reset();
134+
String s = new String(bytes);
135+
if (s.startsWith("Network Error"))
136+
return responseCode = HTTP_NOT_FOUND;
137+
} catch (Exception e) {
138+
exc = e;
139+
}
140+
return responseCode = HTTP_INTERNAL_ERROR;
141+
}
142+
@Override
143+
public void disconnect() {
144+
// TODO Auto-generated method stub
145+
146+
}
147+
148+
@Override
149+
public boolean usingProxy() {
150+
// TODO Auto-generated method stub
151+
return false;
152+
}
153+
110154
}

0 commit comments

Comments
 (0)