我目前正在嘗試通過http獲取調用與服務器進行身份驗證。下面提供的代碼在java項目中編譯時工作。將正確的標記返回到程序。然而,無論何時我嘗試在Android中實現相同的代碼,我都不會收到通過Get調用返回的令牌。HTTPS請求,Android中的身份驗證
在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();
}
}
}
感謝您的幫助!
?訪問日誌,狀態代碼等 – 2010-06-22 15:55:10
不幸的是,服務器託管在外 – Mango 2010-06-23 07:24:18
當我在Android中運行代碼時,它似乎總是在「InputStream ins - con.getInputStream();」行中失敗。它拋出IOException。但是,當我將代碼作爲Java應用程序運行時,不會發生這種情況。 – Mango 2010-06-23 08:12:32