【发布时间】:2012-08-10 12:10:31
【问题描述】:
我正在从我的 GWT-Client 提交 JSON 数据并将其传递给我的 GWT-Server。我想将数据重新提交到另一台服务器,并希望从另一台服务器响应 GWT-Client。
我只是不知道我该怎么做。我尝试了下面的代码,但没有工作。
我的代码是:
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("POST");
StringBuffer jb = new StringBuffer();
URL oracle = new URL("http://www.google.com");
HttpURLConnection connection = null;
connection = (HttpURLConnection) oracle.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setDoInput(true);
request.getInputStream();
OutputStream wr = connection.getOutputStream();
InputStream in = request.getInputStream();
byte[] buffer = new byte[512];
int read = in.read(buffer, 0, buffer.length);
while (read >= 0) {
wr.write(buffer, 0, read);
read = in.read(buffer, 0, buffer.length);
}
wr.flush();
wr.close();
BufferedReader in1 = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String inputLine;
while ((inputLine = in1.readLine()) != null) {
jb.append(inputLine);
}
response.setContentType("text/html");
// Get the printwriter object from response to write the required json
// object to the output stream
PrintWriter out = response.getWriter();
// Assuming your json object is **jsonObject**, perform the following,
// it will return your json object
out.print(jb.toString());
out.flush();
in1.close();
}
请帮帮我。
【问题讨论】:
-
这会是 HTML5 Web 套接字的理想选择吗?
-
no.. 我不想使用 HTML5 网络套接字,因为它不支持 IE6 :(
-
如果人们强制支持 IE6 就换工作。这是不人道的。
-
我在使用 GWT 和 IE6 方面有过不好的体验
-
我还建议在这种情况下考虑异步响应(请参阅stackoverflow.com/questions/12085235/…)
标签: java http gwt servlets http-headers