/// <summary> /// Creates the push request. /// </summary> /// <param name="request">The request.</param> /// <returns>HttpWebRequest.</returns> protected HttpWebRequest CreatePushRequest(PushMessageRequestV3 request) { HttpWebRequest httpRequest = null; if (request != null) { httpRequest = (HttpWebRequest)HttpWebRequest.Create(apiBaseUrl + "push"); FillAuthenticationV3(httpRequest); var jsonObject = new JObject(); jsonObject.Add(request.Platform.ToJson()); jsonObject.Add(request.Audience.ToJson()); if (request.Notification == null && request.AppMessage == null) { throw ExceptionFactory.CreateInvalidObjectException(nameof(request), reason: "Notification/AppMessage"); } if (request.Notification != null) { jsonObject.Add(new JProperty("notification", JObject.FromObject(request.Notification))); } if (request.AppMessage != null) { jsonObject.Add(new JProperty("message", JObject.FromObject(request.AppMessage))); } jsonObject.Add(new JProperty("options", JObject.FromObject(CreateRequestOptions(request)))); httpRequest.FillData(HttpConstants.HttpMethod.Post, jsonObject.ToString()); } return(httpRequest); }
/// <summary> /// Fills the HTTP request to HTTP web request. /// </summary> /// <param name="httpRequest">The HTTP request.</param> /// <param name="request">The request.</param> /// <param name="rewriteDelegate">The rewrite delegate.</param> public static void FillHttpRequestToHttpWebRequest(this HttpRequest httpRequest, HttpWebRequest request, Func <NameValueCollection, NameValueCollection, Exception> rewriteDelegate = null) { if (httpRequest != null && request != null) { //Copy header request.Headers.Clear(); foreach (var key in httpRequest.Headers.AllKeys) { request.SafeSetHttpHeader(key, httpRequest.Headers.Get(key)); } if (rewriteDelegate != null) { var exception = rewriteDelegate(request.Headers, httpRequest.Headers); if (exception != null) { throw exception.Handle(); } } //Copy body, for PUT and POST only. if (httpRequest.HttpMethod == HttpConstants.HttpMethod.Put || httpRequest.HttpMethod == HttpConstants.HttpMethod.Post) { var bytes = httpRequest.InputStream.ReadStreamToBytes(true); request.FillData(httpRequest.HttpMethod, bytes, httpRequest.ContentType); } } }
/// <summary> /// Posts the specified request data. /// </summary> /// <param name="requestData">The request data.</param> /// <param name="gateway">The gateway.</param> /// <param name="charSet">The character set.</param> /// <returns>System.String.</returns> /// <exception cref="System.Exception"></exception> private static string GetResponseData(Dictionary <string, string> requestData, string gateway = null, Encoding charSet = null) { if (gateway == null) { gateway = AliServiceConfig.AliCellphoneGateway; } if (charSet == null) { charSet = Encoding.GetEncoding(AliServiceConfig.InputCharset); } WebResponse response = null; try { HttpWebRequest request = gateway.CreateHttpWebRequest(gateway); request.FillData("POST", requestData, charSet); response = request.GetResponse(); return(response.ReadAsText(charSet)); } catch (Exception ex) { throw new Exception(ex.Message, ex); } finally { if (response != null) { response.Close(); } } }