HTTPS请求,在Android的认证 [英] Https request, authentication in Android

查看:149
本文介绍了HTTPS请求,在Android的认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在试图通过一个HTTP GET调用服务器进行身份验证。当一个Java项目编译下面提供的code工作。返回正确的令牌程序。但是每当我试图实现相同的code在Android中我没有得到通过get调用返回的令牌。

I am currently trying to authenticate with a server via a http Get call. The code provided below works when compiled in a java project. Returning the correct token to the program. However whenever I try to implement the same code in Android I do not get a token returned via the Get call.

在Android的,我回来inputLine的功能,但是inputLine始终是一个空字符串。

In Android I am returning inputLine in a function, however inputLine is always an empty string.

本的System.out.println()打印返回的标记。

The system.out.println() prints the returned token.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class JavaHttpsExample 
{

public static void main(String[] args)
{   String inputLine = new String();
    try
    {
    String httpsURL = "https://the url";
    URL myurl = new URL(httpsURL);
    HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
    InputStream ins = con.getInputStream();
    InputStreamReader isr=new InputStreamReader(ins);
    BufferedReader in =new BufferedReader(isr);

    inputLine = in.readLine();

    System.out.println(inputLine);

    in.close();


    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
 }
}

感谢您的帮助!

thanks for your help!!!

推荐答案

我使用POST和FormEntity从服务器(例如身份验证)检索数据,我从未有过任何问题:

I'm using POST and FormEntity for retrieving data from the server (such as authentication), and i have never had any problems:

final String httpsURL = "https://the url";
final DefaultHttpClient client = new DefaultHttpClient();
final HttpPost httppost = new HttpPost(httpsURL);

//authentication block:
final List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("userName", userName));
nvps.add(new BasicNameValuePair("password", password));
final UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
httppost.setEntity(p_entity);

//sending the request and retrieving the response:
HttpResponse response = client.execute(httppost);
HttpEntity responseEntity = response.getEntity();

//handling the response: responseEntity.getContent() is your InputStream
final InputSource inputSource = new InputSource(responseEntity.getContent());
[...]

也许你会发现这个有用

maybe you'll find this usefull

这篇关于HTTPS请求,在Android的认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆