![](/img/trans.png)
[英]Android Exception(Not trusted server certificate) : https XML request with server authentication
[英]Https request, authentication in Android
我目前正在嘗試通過http Get調用服務器進行身份驗證。 下面提供的代碼在java項目中編譯時有效。 將正確的令牌返回給程序。 但是,每當我嘗試在Android中實現相同的代碼時,我都不會通過Get調用返回令牌。
在Android中,我在函數中返回inputLine,但inputLine始終是空字符串。
system.out.println()打印返回的標記。
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();
}
}
}
謝謝你的幫助!!!
您可能沒有將Internet-Permission添加到項目AndroidManifest.xml中。 如果是這樣,請將以下行添加為<manifest/>
節點的子級:
<uses-permission android:name="android.permission.INTERNET" />
我正在使用POST和FormEntity從服務器檢索數據(例如身份驗證),我從來沒有遇到任何問題:
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());
[...]
也許你會發現這很有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.