/// <summary> /// application/x-www-form-urlencode 提交方式 /// </summary> /// <param name="url">提交eSight的url</param> /// <param name="nameValueCollection">key/value值对对象</param> /// <param name="isOpenAgain">重新打开连接</param> /// <returns>eSight返回结果,JObject</returns> private JObject HCPostForm(string url, IEnumerable <KeyValuePair <string, object> > nameValueCollection, bool isOpenAgain) { if (isOpenAgain) { Open(); } string abUrl = GetFullURL(url); InitHCHead(); JObject retObj = null; try { for (int i = 0; i <= 1; i++) { var content = new FormUrlEncodedContentEx(nameValueCollection);//, Encoding.UTF8, "application/x-www-form-urlencoded"); LogUtil.HWLogger.API.DebugFormat("Send json by PostForm[{0}]:{1}", abUrl, GetPrintInfoOfJson(nameValueCollection)); HttpResponseMessage hrm = HClient.PostAsync(abUrl, content).Result; retObj = HCCheckResult(abUrl, hrm); if (!string.Equals(GetJObjectPropVal <string>(retObj, "code"), Const.ConstMgr.ErrorCode.HW_LOGIN_AUTH)) { break; } else { if (isOpenAgain) { LogUtil.HWLogger.API.WarnFormat("Login agin,Retry.."); TryOpen(); } else { break; } } } } catch (System.AggregateException ae) { HandleException(ae); } catch (Exception se) { LogUtil.HWLogger.API.Error(se); throw; } return(retObj); }
private static byte[] GetContentByteArray(IEnumerable <KeyValuePair <string, string> > nameValueCollection) { StringBuilder stringBuilder = new StringBuilder(); using (IEnumerator <KeyValuePair <string, string> > enumerator = nameValueCollection.GetEnumerator()) { while (enumerator.MoveNext()) { KeyValuePair <string, string> current = enumerator.Current; if (stringBuilder.Length > 0) { stringBuilder.Append('&'); } stringBuilder.Append(FormUrlEncodedContentEx.Encode(current.Key)); stringBuilder.Append('='); stringBuilder.Append(FormUrlEncodedContentEx.Encode(current.Value)); } } return(Encoding.UTF8.GetBytes(stringBuilder.ToString())); }
public FormUrlEncodedContentEx(IEnumerable <KeyValuePair <string, string> > nameValueCollection) : base(FormUrlEncodedContentEx.GetContentByteArray(nameValueCollection)) { base.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); }