0

I'm not quite sure how it works but what I need to do is authenticate with JSON-RPC and then request a page. I have this code to find out if the username and password is right but I don't know how to use this information to request a page..

    List<Cookie> cook;
    public void postData(String method, Object ... params) throws JSONException {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.examp;e.com/json-rpc/");

        try {
            // Add your data

            String methodandp = "{id:3, method:" + method + ", params:[" + params[0].toString()+ ", " + params[1].toString() + "]}";

            StringEntity a = new StringEntity(methodandp);
            httppost.setEntity(a);
            Log.i("ClientActivity: request", httppost.getMethod().toString());
            Log.i("ClientActivity: request", nameValuePairs.toString());


            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);


            Header[] head = response.getAllHeaders();

            Log.i("ClientActivity", response.getStatusLine().toString());
            String responseString = EntityUtils.toString(response.getEntity());
            responseString = responseString.trim();
            JSONObject jsonResponse = new JSONObject(responseString);
            Log.i("ClientActivity", jsonResponse.toString());
            String cookie = "";

            for(int i = 0; i < head.length; i++){
                Log.i("ClientActivity: response", head[i].toString());
                if(head[i].toString().contains("Set-Cookie")){
                    cookie = (head[i].toString()).replace("Set-Cookie: ", "");
                    cook = ((AbstractHttpClient) httpclient).getCookieStore().getCookies();
                    if (cook.isEmpty()) {
                        System.out.println("None");
                    } else {
                        for (int x = 0; x < cook.size(); x++) {
                            Log.i("ClientActivity", "-" + cook.get(x).toString());
                        }
                    }
                    Log.i("ClientActivity: Cookie", cookie);
                }
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    } 

It comes back with successful and with all the right data but I dont know how to use it.

FabianCook
  • 20,269
  • 16
  • 67
  • 115
  • 1
    A lot of that code isn't really relevant for extracting the cookie from the response, so maybe you should clean that up first. Now if you have finished the for loop, you should normally have (normally means: perform an empty check on the cookie String) the cookie in the so-called variable. Then, you can create a GET request, set a header containing this cookie, and retrieve the required web page. – Rob Apr 14 '12 at 01:39
  • Okay I have done that, but now how do I get the content that I just requested ? – FabianCook Apr 14 '12 at 02:00
  • Can you try to clarify your actual question? What exactly are you trying to do with the data retrieved from that URL? Trying to forward to a different page? – robertvoliva Apr 14 '12 at 03:44
  • Also `methodandp` looks like it doesn't quote `method` correctly -- i.e: invalid JSON in your JSON-RPC envelope. – David-SkyMesh Apr 14 '12 at 07:56
  • The method comes out right, it returns the right data, I am trying to use the data here to get another page – FabianCook Apr 14 '12 at 11:45

1 Answers1

1

This was actually really simple now that I look at it, once I have verified that I was authenticated I could use the same HttpClient (that holds the cookies) to request other files and push files to the web server using MultipartEntity

FabianCook
  • 20,269
  • 16
  • 67
  • 115