在開發開心網「爭車位」輔助工具的過程中,貼條功能雖無重大技術難點,但其業務邏輯值得記錄。核心在於判斷車位是否符合貼條條件:車位必須有車輛停放,且車位利潤需超過 150 點。
////// 對指定車位進行貼條操作 /// /// 目標車位資訊 public PostResult PostOneCar(ParkingInfo parkingInfo) { // 車位無車輛,直接返回 if (parkingInfo.CarId.Equals("0")) return null; // 車位利潤不足 150,不貼條 if (!string.IsNullOrEmpty(parkingInfo.CarProfit) && Convert.ToInt32(parkingInfo.CarProfit) <= 150) return null; string postUrl = "https://www.kaixin001.com/parking/post.php"; string postParams = string.Format("_=&acc={0}&parkid={1}&verify={2}", acc, parkingInfo.ParkId, verify); string postResultJson = new HttpHelper().GetHtml(postUrl, postParams, true, Utility.Cookies); if (string.IsNullOrEmpty(postResultJson)) return null; // 處理 JSON 回傳資料 object o = Newtonsoft.Json.JavaScriptConvert.DeserializeObject( postResultJson.Replace("\\/", "/").Replace("\\u", "\\\\u")); return new PostResult(o); }
測試程式碼模擬登入後,針對第 4 個車位(索引 3)執行貼條操作,並驗證回傳錯誤代碼為 "0" 代表成功:
[TestMethod()]
public void PostOneCarTest()
{
string loginEmail = UnitTestHelper.LoginEmail;
string loginPassword = UnitTestHelper.LoginPassword;
Utility.Login(loginEmail, loginPassword);
ParkingHelper helper = new ParkingHelper();
helper.GetParkerDetails();
PostResult result = helper.PostOneCar(helper.ParkingList[3]);
Assert.AreEqual("0", result.ErrNo);
Console.WriteLine(result.Error);
}
此功能結合 HTTP 模擬請求與 JSON 解析,展示了自動化操作的典型流程。