Android通過https協議與伺服器端進行通訊
Https與Http類似,只不過Https一般是通過post請求伺服器,但是Https與http不同的是Https與伺服器會話是處於連線狀態。http則傳送請求後連線就會斷開。
傳送post請求程式碼:
String query = r4 + "&pass=" + r3; //請求引數
byte[] entitydata = query.getBytes();//得到實體資料
HttpsURLConnection urlCon = (new URL(ticketurl)).openConnection();
urlCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
urlCon.setRequestProperty("Content-Length", String.valueOf(entitydata.length));
((HttpsURLConnection) urlCon).setRequestMethod("POST");
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.connect();
//把封裝好的實體資料傳送到輸出流
OutputStream outStream = urlCon.getOutputStream();
outStream.write(entitydata);
outStream.flush();
outStream.close();
//伺服器返回輸入流並讀寫
BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
return line;
}
in.close();
另外使用HttpsURLConnection時需要實現HostnameVerifier 和 X509TrustManager,這兩個實現是必須的,要不會報安全驗證異常。然後初始化X509TrustManager中的SSLContext,為javax.net.ssl.HttpsURLConnection設定預設的SocketFactory和HostnameVerifier。程式碼如下:
private myX509TrustManager xtm = new myX509TrustManager();
private myHostnameVerifier hnv = new myHostnameVerifier();
public HttpsURLConnectionTest() {
//初始化X509TrustManager中的SSLContext
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
X509TrustManager[] xtmArray = new X509TrustManager[] { xtm };
sslContext.init(null, xtmArray, new java.security.SecureRandom());
} catch (GeneralSecurityException gse) {
}
//為javax.net.ssl.HttpsURLConnection設定預設的SocketFactory和HostnameVerifier
if (sslContext != null) {
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
}
HttpsURLConnection.setD
傳送post請求程式碼:
String query = r4 + "&pass=" + r3; //請求引數
byte[] entitydata = query.getBytes();//得到實體資料
HttpsURLConnection urlCon = (new URL(ticketurl)).openConnection();
urlCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
urlCon.setRequestProperty("Content-Length", String.valueOf(entitydata.length));
((HttpsURLConnection) urlCon).setRequestMethod("POST");
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.connect();
//把封裝好的實體資料傳送到輸出流
OutputStream outStream = urlCon.getOutputStream();
outStream.write(entitydata);
outStream.flush();
outStream.close();
//伺服器返回輸入流並讀寫
BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
return line;
}
in.close();
另外使用HttpsURLConnection時需要實現HostnameVerifier 和 X509TrustManager,這兩個實現是必須的,要不會報安全驗證異常。然後初始化X509TrustManager中的SSLContext,為javax.net.ssl.HttpsURLConnection設定預設的SocketFactory和HostnameVerifier。程式碼如下:
private myX509TrustManager xtm = new myX509TrustManager();
private myHostnameVerifier hnv = new myHostnameVerifier();
public HttpsURLConnectionTest() {
//初始化X509TrustManager中的SSLContext
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
X509TrustManager[] xtmArray = new X509TrustManager[] { xtm };
sslContext.init(null, xtmArray, new java.security.SecureRandom());
} catch (GeneralSecurityException gse) {
}
//為javax.net.ssl.HttpsURLConnection設定預設的SocketFactory和HostnameVerifier
if (sslContext != null) {
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
}
HttpsURLConnection.setD
相關文章
- Oracle Weblogic叢集啟動通過HTTP等協議進行隧道通訊OracleWebHTTP協議
- [譯]通過HTTPS協議執行你的Flask程式HTTP協議Flask
- 【物聯網雲端對接-1】 通過HTTP協議與微軟Azure IoT hub進行雲端通訊HTTP協議微軟
- 通訊協議協議
- Golang 實現客戶端與伺服器端UDP協議連線通訊Golang客戶端伺服器UDP協議
- 通過串列埠進行通訊 :串列埠
- Redis 通訊協議Redis協議
- HTTP通訊協議HTTP協議
- Mysql通訊協議MySql協議
- MQ通訊協議MQ協議
- web通訊協議Web協議
- 【物聯網雲端對接-2】通過MQTT協議與阿里雲物聯網套件進行雲端通訊MQQT協議阿里套件
- 網路通訊協議協議
- Dubbo-通訊協議協議
- 串列埠通訊協議串列埠協議
- TCP協議服務端和客戶端的連線與通訊TCP協議服務端客戶端
- Raft協議:通過TermId大的通過Raft協議
- USB共享網路:android手機通過USB與Ubuntu進行socket網路通訊AndroidUbuntu
- 通訊協議protobuf的原理與實現協議
- MQTT協議 - arduino ESP32 通過精靈一號 MQTT Broker 進行通訊的程式碼詳解MQQT協議UI
- https的通訊過程HTTP
- Android與物聯網裝置通訊-UDP&TCP協議AndroidUDPTCP協議
- 輕量通訊協議 --- MQTT協議MQQT
- HTTP協議的通訊框架HTTP協議框架
- WLAN常用的通訊協議協議
- 通訊協議:HTTP、TCP、UDP協議HTTPTCPUDP
- 15. SPI通訊協議協議
- IIC通訊協議筆記協議筆記
- SPI通訊協議筆記協議筆記
- 網路通訊協議-ICMP協議詳解!協議
- 網路通訊協議-TCP協議詳解!協議TCP
- 網路通訊協議-HTTP協議詳解!協議HTTP
- 網路通訊協議-SMTP協議詳解!協議
- 從PHP客戶端看MongoDB通訊協議TDPHP客戶端MongoDB協議
- 通過Socket進行Http/Https 網頁操作HTTP網頁
- ModBus RTU與ModBus TCP通訊協議詳解TCP協議
- 如何通過CRM進行遠端支援?
- Redis:我是如何與客戶端進行通訊的Redis客戶端