0

I am submitting JSON data from my GWT-Client and Passing it to my GWT-Server. And i want to re-submit data to another server and want response from another server to GWT-Client.

I just don't know how can i do this. I tried below code but not working.

My Code is :

@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();
}

Please help me.

Tushar Ahirrao
  • 10,773
  • 16
  • 61
  • 95

2 Answers2

1

You'll have to send the request to the other server before reading the response

URL oracle = new URL("http://www.anotherserver.com/");

HttpURLConnection connection = null;
connection = (HttpURLConnection) oracle.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);

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 in = new BufferedReader(new InputStreamReader(
        connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
    jb.append(inputLine);
}

see also Using java.net.URLConnection to fire and handle HTTP requests

Community
  • 1
  • 1
andih
  • 5,330
  • 3
  • 25
  • 35
  • @andih..thanks for answer..but where is String "query" and from where "postParameters" comes??? – Tushar Ahirrao Aug 14 '12 at 08:44
  • `postParameter` was a copy and paste error. I've removed it. `query` is generated from the original post parameters. For simplicity I've created query from only two parameters which are initialized with the parameters from the origianal request. In productive code you would generate the query String using a loop over all parameters found within the parameterMap – andih Aug 14 '12 at 08:51
  • @andih...but i am sending json from client side like this " {temp:'123'} so how can i encode and send to another server?? – Tushar Ahirrao Aug 14 '12 at 08:56
  • I've replaced my copy post parameters by copy the request body. You should consider setting the correct encoding i.e. the encoding of the original request before you copy the body content to the "other" server. – andih Aug 14 '12 at 09:08
  • @audih..I replaced my code in question..please check it..but still am receiving error "java.io.IOException: Server returned HTTP response code: 405 for URL: http://www.google.com" – Tushar Ahirrao Aug 14 '12 at 09:22
  • @audhi..Hey thanks dude..its working now with my actual server... :) – Tushar Ahirrao Aug 14 '12 at 09:28
1

In addition, you can use Apache httpclient, it's very simple to implement your requirement, such as :

public static void main(String[] args) {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://www.anotherserver.com/");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("key",
                "value"));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}
Jason
  • 1,167
  • 7
  • 15