Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从HTTP请求设置cookie_Java_Android - Fatal编程技术网

Java 从HTTP请求设置cookie

Java 从HTTP请求设置cookie,java,android,Java,Android,我已经使用HttpRequest获得了正确的登录。它在MyToast中打印logn页面的正确html格式(仅用于测试)。现在我想根据该请求设置一个cookie。这怎么可能? 如果有必要,我可以提供一些代码 我已经知道CookieManager类,但我如何才能成功地做到这一点 提前谢谢 我的代码: public String getPostRequest(String url, String user, String pass) { HttpClient postClient =

我已经使用HttpRequest获得了正确的登录。它在MyToast中打印logn页面的正确html格式(仅用于测试)。现在我想根据该请求设置一个cookie。这怎么可能? 如果有必要,我可以提供一些代码

我已经知道CookieManager类,但我如何才能成功地做到这一点

提前谢谢

我的代码:

    public String getPostRequest(String url, String user, String pass) {
    HttpClient postClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    HttpResponse response;

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("login", user));
        nameValuePairs.add(new BasicNameValuePair("pass", pass));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));      

        response = postClient.execute(httpPost);

        if(response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                InputStream instream = entity.getContent();  
                String result = convertStreamToString(instream);                   
                instream.close();             

                return result;      
            }
        }
    } catch (Exception e) {}
    Toast.makeText(getApplicationContext(),
            "Connection failed",
            Toast.LENGTH_SHORT).show();
    return null; 
}

private String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return sb.toString();
}           
公共字符串getPostRequest(字符串url、字符串用户、字符串传递){
HttpClient postClient=新的默认HttpClient();
HttpPost HttpPost=新的HttpPost(url);
HttpResponse响应;
试一试{
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“登录”,用户));
添加(新的BasicNameValuePair(“pass”,pass));
setEntity(新的UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
response=postClient.execute(httpPost);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity=response.getEntity();
如果(实体!=null){
InputStream instream=entity.getContent();
字符串结果=convertStreamToString(流内);
流内关闭();
返回结果;
}
}
}捕获(例外e){}
Toast.makeText(getApplicationContext(),
“连接失败”,
吐司。长度(短)。show();
返回null;
}
私有字符串convertStreamToString(InputStream为){
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
StringBuilder sb=新的StringBuilder();
字符串行=null;
试一试{
而((line=reader.readLine())!=null){
某人附加(行);
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
is.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
使某人返回字符串();
}           
嗯,差不多就是这样。convertStreamToString()函数将InputStream转换为字符串(纯HTML代码),我将其“toast”出来只是为了测试它(这样它就可以工作),所以代码仍然可以工作。现在设置cookie.:-)

这就是我目前的目标:

// inside my if (entity != null) statement
List<Cookie> cookies = postClient.getCookieStore().getCookies();
String result = cookies.get(1).toString();
                    return result;
//在我的if(entity!=null)语句中
列出cookies=postClient.getCookieStore().getCookies();
字符串结果=cookies.get(1.toString();
返回结果;

登录后,CookieList id 1包含一个值,否则该值为标准值。所以现在我知道了价值上的差异,但我如何才能继续呢?

我认为Android附带了ApacheHttpClient 4.0

您可以查看HttpClient教程中的主题

您也可以参考类似的问题,以便:


还要检查此示例的用法:

好吧,我现在已经研究过了,但我不知道如何实现(因为使用响应来设置cookie)。检查此示例的用法:如果教程和示例中的信息不起作用,则共享代码。您是否尝试过任何失败的cookie?请包括在内。我认为教程和示例包含了足够的信息,可以帮助您理解:)。好吧,我想在if(entity!=null)语句中设置cookie。但是,如果登录失败,请不要设置cookie,否则请设置。问题在于如何实施它。我对这真的很陌生。