基于HttpWebRequest,HttpWebResponse发起请求
/// <summary> /// 获取版本更新信息 GET /// </summary> /// <param name="softwareKey">软件的标识</param> /// <returns></returns> public static ResponseResult<UpdateInfo> GetUpdateInfo(string softwareKey) { ResponseResult<UpdateInfo> updateInfo = new ResponseResult<UpdateInfo>(); if (!IsConnectInternet()) { updateInfo.statusCode = 404; return updateInfo; } string updateInfoStr = null; #if DEBUG Uri address = new Uri("http://xxxx/Api/UpdateCenter/GetUpdateInfo?softwareKey=" + softwareKey); #else Uri address = new Uri("http://xxxx/Api/UpdateCenter/GetUpdateInfo?softwareKey=" + softwareKey); #endif try { // 使用httpwebrequest调用api接口 HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(address); httpWebRequest.Method = "GET"; HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()); updateInfoStr = streamReader.ReadToEnd(); if (!string.IsNullOrWhiteSpace(updateInfoStr)) { updateInfo = JsonConvert.DeserializeObject<ResponseResult<UpdateInfo>>(updateInfoStr); return updateInfo; } } catch (Exception ex) { } return null; }
/// <summary> /// 报送版本号与序列号 POST /// </summary> public static void PostVersionSerial() { Thread thread = new Thread(() => { try { string result = ""; string JsonStr ="'"+SystemConfigBLL.GetSingle("CompanyInfo").Value+"'"; #if DEBUG Uri uri = new Uri("http://xxxxx/Api/Customer/Report"); #else Uri uri = new Uri("http://xxxxx/Api/Customer/Report"); #endif
//向接口发送数据 并得到返回值 HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(uri); objRequest.Method = "POST"; objRequest.ContentType = "application/json"; byte[] byteData = Encoding.UTF8.GetBytes(JsonStr); objRequest.ContentLength = byteData.Length; objRequest.GetRequestStream().Write(byteData, 0, byteData.Length); HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) { result = sr.ReadToEnd(); sr.Close(); } ResponseResults<CustomerCode> responseResults = JsonConvert.DeserializeObject<ResponseResults<CustomerCode>>(result); if (!string.IsNullOrEmpty(responseResults.content.code)) { if (string.IsNullOrEmpty(SystemConfigBLL.GetSingle("CustomerCode").Value)) { int res = SystemConfigBLL.Update("CustomerCode", responseResults.content.code); } } } catch { } }); thread.IsBackground = true; thread.Start(); }