這篇部落格講的是客戶端如何封裝Http協議,客戶端如何使用post,get方法,是最最核心的程式碼之一
以下摘自黃石君的《android與PHP開發》
package com.app .demos.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List; util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatimache. http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpacheGet; http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.params.ConnRoaultPNames;
import org.apache.http.impl. org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org. apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import com.app.demos.base.C;
import android.util.Log;
@SuppressWarnings("rawtypes")
public@SuppressWarnings("rawtypes")
public@SuppressWarnings("rawtypes")
public@SuppressWarnings("rawtypes")
public@SuppressWarnings("rawtypes")
public@SuppressWarnings("rawtypes")
public@SuppressWarnings("rawtypes") class AppClient {
// compress strategy
final private static int CS_NONE = 0;finalfinal private stat int
// logic variables
private String apiUrl;
private HttpParams httpParams;
private HttpClient httpClient;
private int timeoutConnection = 100000; private int compress = CS_NONE;
/ / charset default utf8
private String charset = HTTP.UTF_8;
public AppClient (String Cl
public AppClient (String url, String charset, int compress) {
initClient(url);
this.charset = charset; // set charset
this。
private void initClient (String url) {
// initialize API URL
this.apiUrl = C.api.base + url;
this.apiUrl = C.api.base + url; this.apiUrl = C.api.base + url;
this.apiUrl = C.api.base + url; ? c.api.base c是自己建造的一個類別裡面
package com.app.demos.base;
公開最終課程C {
////////////////////////// / ///////////////////////////////////////////////// ////////// ////////////////////
// 核心設定(重要)
publicstatstatic Final class dir {
public static final String base = "/sdcard/demos";
公共靜態最終字串面孔= 基礎+ "/faces";公共靜態最終字符串面孔
= 基礎+ "/faces"; = 基礎+ "/images";}
公共靜態最終類API {
公共靜態最終字串基礎
=“http://192.168. 公用靜態最終字串索引=“/index/ index";
public static final String 登入= "/index/login";
public static Final Final String Final Final. logout";
public static final String faceView = "/image/faceView";
公共靜態最終字符串faceList=“/image/ListList”;公共靜態最終字符串faceList=“/image/ListList”;
=“/blog/blogList”;公共靜態最終字串blogView
= "/blog/blogView";public static final String blogCreate
public static final String blogCreate public static final String commentList
= "/comment/commentList"; public static final String commentCreate
= "/comment/commentCreate"; " ;public static final String customerEdit
= "/customer/customerEdit" ;公共靜態最終字串fansAdd
=“/customer/fansAdds”; “/customer/fansDel”;公共靜態最終字串通知
=“/通知/通知";}
= 1001;公共靜態最終int登入
= 1002; 公共靜態最終int 註銷面
= 1003;公共靜態最終int 註銷面= 1003;
公共靜態最終int 面列表
= 1005;
公共靜態最終int blogList= 1006 ;
public static final int blog = 1008;
public static final int commentList= 1009;
public static final int commentCreate =1010 ;
= 1012; 公共靜態最終粉絲添加
= 1013;公共靜態最終粉絲D埃爾
= 1014;
公用靜態最終類別錯誤{公用靜態最終字串網路
= "網路錯誤"; public static final String message
= "訊息錯誤" 是public static final String message= "訊息錯誤"; = "訊息格式錯誤";
}
////////// //////////////////////// //////////////////////////////////// /////////////// ///////////////////////
//意圖和操作設置
公共靜態最終類意圖{
公共靜態最終類別操作{公共靜態最終字串EDITTEXT
=“com.app.demos.EDITTEXT”;
公共靜態最終字串EDITBLOG=“ com.app.demos.de. }
}
public static tinal Class Action {公共靜態最終類EDITTEXT {inal final f MMENT
= 2002;}
}
//////////////////////////////////////////// ////////////////////////////////////////////////// //
// additional settings
public static final class web {
public static final String public. static final String index
= base + "/index.php";public static final String gomap
= base + "/gomap.php";}
String apiSid = AppUtil
.getSessionId(); null && apiSid.length() > 0) {
}
// set client timeout HttpConnectionParams.setConnectionTimeout(httpParams, timeoutConnection);HttpConnectionParams.setSoTimeout(httpParams, timeoutSocket);s); //
此處是簡單的取得Httpclient
}
public void useWap () { //此處是選擇wap的上網方式
HttpHost proxy = new HttpHost("10.0.0.172", 80, "http");set
httpClient.getParames(). );
}
public String get () throws Exception { //封裝Http的get方法
(this.apiUrl)) ;Log.w("AppClient.get.url", this.apiUrl);
// send get request
HttpResponse httpResponse = httpCl
HttpResponse httpResponse = httpClnient. ().getStatusCode() == HttpStatus.SC_OK) {
String httpResult = resultFilter(httpResponse.getEntity());
Log.w("AppClient.get.result", httpRes7); httpResult;
} else {return null;
}
} catch (ConnectTimeoutException e. ception e ) {
e.printStackTrace();
}
return null;
}
ttp的post方法
try {
HttpPost httpPost = headerFilter(new HttpPost(this.apiUrl));
List
Iterator it = urlParams.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();Map.Entry entry = (Map.Entry) it.next();
(new BasicNameValuePair(entry.getKey().toString(), entry.getValue().toString()));
}
// set data charset
{
httpPost.setEntity(new UrlEncodedFormEntity(postParams, this.charset));
} else {
posthttpPost.setEntity(new Urla); Log.w( "AppClient.post.url", this.apiUrl);
Log.w("AppClient.post.data", postParams.toString());// send post request .execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String httpResult = resultFilter(httpResponse.Ent AppClient.post.result", httpResult);
return httpResult;
} else {
return null;? new Exception(C .err.network);
} catch (Exception e) {
e.printStackTrace();
}
return null;private HttpGet headerFilter (HttpGet httpGet) { 處理發送get資料請求,看是否是壓縮格式
switch (this.compress) {
case CS_GZIP:
httpGet.addHeader("Accept-ipco), break;
default :
break;
}
return httpGet;
) { 處理發送post資料請求,看是否是壓縮格式
switch (this.compress) {
case CS_GZIP:httpPost.addHeader("Accept-Encoding", "gzip");httpPost.addHeader("Accept-Encoding", "gzip"); break;
}
return httpPost;
}
private String resultFilter(
String result = null;
try {
switch (this.compress) {
case CS_GZIP:
result = AppUtil.gzipToString(oity); result = EntityUtils. toString(entity);break;
}
} catch (IOException e) {
e.printStackTrace();
}
以上就介紹了微博開發1客戶端的http的get和post封裝,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

要保護應用免受與會話相關的XSS攻擊,需採取以下措施:1.設置HttpOnly和Secure標誌保護會話cookie。 2.對所有用戶輸入進行輸出編碼。 3.實施內容安全策略(CSP)限制腳本來源。通過這些策略,可以有效防護會話相關的XSS攻擊,確保用戶數據安全。

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显著提升应用在高并发环境下的效率。

theSession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceisesneededeededeedeedeededto toavoidperformance andunununununexpectedLogOgouts.3)

在PHP中,可以使用session_name()函數配置會話名稱。具體步驟如下:1.使用session_name()函數設置會話名稱,例如session_name("my_session")。 2.在設置會話名稱後,調用session_start()啟動會話。配置會話名稱可以避免多應用間的會話數據衝突,並增強安全性,但需注意會話名稱的唯一性、安全性、長度和設置時機。

會話ID應在登錄時、敏感操作前和每30分鐘定期重新生成。 1.登錄時重新生成會話ID可防會話固定攻擊。 2.敏感操作前重新生成提高安全性。 3.定期重新生成降低長期利用風險,但需權衡用戶體驗。

在PHP中設置會話cookie參數可以通過session_set_cookie_params()函數實現。 1)使用該函數設置參數,如過期時間、路徑、域名、安全標誌等;2)調用session_start()使參數生效;3)根據需求動態調整參數,如用戶登錄狀態;4)注意設置secure和httponly標誌以提升安全性。

在PHP中使用會話的主要目的是維護用戶在不同頁面之間的狀態。 1)會話通過session_start()函數啟動,創建唯一會話ID並存儲在用戶cookie中。 2)會話數據保存在服務器上,允許在不同請求間傳遞數據,如登錄狀態和購物車內容。

如何在子域名間共享會話?通過設置通用域名的會話cookie實現。 1.在服務器端設置會話cookie的域為.example.com。 2.選擇合適的會話存儲方式,如內存、數據庫或分佈式緩存。 3.通過cookie傳遞會話ID,服務器根據ID檢索和更新會話數據。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Linux新版
SublimeText3 Linux最新版