簡體   English   中英

HTML,身份驗證。 獲取cookie

[英]Html, auth. Getting cookies

我不太確定它是如何工作的,但是我需要做的是使用JSON-RPC進行身份驗證,然后請求一個頁面。 我有這段代碼來查找用戶名和密碼是否正確,但是我不知道如何使用此信息來請求頁面。

    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
        }
    } 

它返回成功並具有所有正確的數據,但我不知道如何使用它。

現在來看,這實際上真的很簡單,一旦我驗證了身份,就可以使用相同的HttpClient(保存cookie)來請求其他文件,並使用MultipartEntity將文件推送到Web服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM