Https request, authentication in Android

前端 未结 2 1587
Happy的楠姐
Happy的楠姐 2021-01-13 04:10

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 pr

相关标签:
2条回答
  • 2021-01-13 04:14

    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

    0 讨论(0)
  • 2021-01-13 04:23

    You probably did not add the Internet-Permission to your projects AndroidManifest.xml. If so, add the following line as a child of the <manifest/> node:

    <uses-permission android:name="android.permission.INTERNET" />
    
    0 讨论(0)
提交回复
热议问题