/// <summary> /// Adds the elements of the specified collection as an application/x-www-form-urlencoded string. /// </summary> /// <param name="content">The collection whose elements should be added.</param> /// <exception cref="ArgumentNullException">The <paramref name="content"/> is null.</exception> public void AddUrlEncoded(IEnumerable <KeyValuePair <string, string> > content) { if (content is null) { throw new ArgumentNullException(nameof(content)); } var ms = new MemoryStream(); using (var w = new StreamWriter(ms, Encoding.ASCII, 1024, true)) { foreach (KeyValuePair <string, string> kvp in content) { if (kvp.Key is null) { if (kvp.Value is null) { continue; } } else { w.Write(Uri.EscapeDataString(kvp.Key)); } if (kvp.Value != null) { w.Write('='); w.Write(Uri.EscapeDataString(kvp.Value)); } w.Write('&'); } w.Flush(); } using (var dataElt = new CefPostDataElement()) { fixed(byte *buffer = ms.GetBuffer()) { // copy (ms.Length - 1) bytes to ignore last ampersand dataElt.SetToBytes(ms.Length > 0 ? ms.Length - 1 : 0, new IntPtr(buffer)); } AddElement(dataElt); } }
/// <summary> /// Retrieve the post data elements. /// </summary> public unsafe virtual void GetElements(ref long elementsCount, ref CefPostDataElement[] elements) { var c1 = new UIntPtr((uint)elements.Length); cef_post_data_element_t **arr1 = (cef_post_data_element_t **)Marshal.AllocHGlobal(sizeof(cef_post_data_element_t *) * elements.Length); for (int i = 0; i < elements.Length; i++) { var e1 = elements[i]; *(arr1 + i) = e1 != null?e1.GetNativeInstance() : null; } NativeInstance->GetElements(&c1, arr1); elementsCount = (long)c1; for (int i = (int)c1; i >= 0; i--) { elements[i] = CefPostDataElement.Wrap(CefPostDataElement.Create, *(arr1 + i)); } Marshal.FreeHGlobal((IntPtr)arr1); GC.KeepAlive(this); }
/// <summary> /// Adds the elements of the specified <see cref="NameValueCollection"/> as an application/x-www-form-urlencoded string. /// </summary> /// <param name="content">The collection whose elements should be added.</param> /// <exception cref="ArgumentNullException">The <paramref name="content"/> is null.</exception> public void AddUrlEncoded(NameValueCollection content) { if (content is null) { throw new ArgumentNullException(nameof(content)); } var ms = new MemoryStream(); using (var w = new StreamWriter(ms, Encoding.ASCII, 1024, true)) { foreach (string key in content.Keys) { string safeKey = key is null ? null : Uri.EscapeDataString(key); foreach (string value in content.GetValues(key)) { w.Write(safeKey); if (value != null) { w.Write('='); w.Write(Uri.EscapeDataString(value)); } w.Write('&'); } } w.Flush(); } using (var dataElt = new CefPostDataElement()) { fixed(byte *buffer = ms.GetBuffer()) { // copy (ms.Length - 1) bytes to ignore last ampersand dataElt.SetToBytes(ms.Length > 0 ? ms.Length - 1 : 0, new IntPtr(buffer)); } AddElement(dataElt); } }
/// <summary> /// Add the specified post data element. Returns true (1) if the add succeeds. /// </summary> public unsafe virtual bool AddElement(CefPostDataElement element) { return(SafeCall(NativeInstance->AddElement((element != null) ? element.GetNativeInstance() : null) != 0)); }