我正在从我的 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();
}
请帮我。