/// <summary> /// Sends a web request with the data either in the query string or in the body. /// </summary> /// <param name="url"> /// The URL to send the request to. /// </param> /// <param name="data"> /// The data to send. /// </param> /// <param name="method"> /// The HTTP method (e.g., GET, POST) to use when sending the request. /// </param> /// <param name="sendInBody"> /// Send the data as part of the body (or in the query string)? /// </param> /// <param name="options"> /// Optional. Additional options. /// </param> /// <returns> /// An object containing details about the result of the attempt to send the data. /// </returns> /// <remarks> /// Parts of this function are from: http://stackoverflow.com/a/9772003/2052963 /// and http://stackoverflow.com/questions/14702902 /// </remarks> public static SendDataResult SendData(string url, IDictionary <string, string> data, string method, bool sendInBody, SendDataOptions options = null) { // Construct a URL, possibly containing the data as query string parameters. var sendInUrl = !sendInBody; var sendDataResult = new SendDataResult(); var uri = new Uri(url); var bareUrl = uri.GetLeftPart(UriPartial.Path); var strQueryString = ConstructQueryString(uri, data); var hasQueryString = !string.IsNullOrWhiteSpace(strQueryString); var requestUrl = hasQueryString && sendInUrl ? $"{bareUrl}?{strQueryString}" : url; // Attempt to send the web request. try { // Construct web request. var request = (HttpWebRequest)WebRequest.Create(requestUrl); request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; request.AllowAutoRedirect = false; request.UserAgent = DefaultUserAgent; request.Method = method; // Add headers? if (options?.Headers != null) { foreach (var header in options.Headers) { request.Headers.Add(header.Key, header.Value); } } // Send the data in the body (rather than the query string)? if (sendInBody) { var postBytes = Encoding.UTF8.GetBytes(strQueryString); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = postBytes.Length; var postStream = request.GetRequestStream(); postStream.Write(postBytes, 0, postBytes.Length); } // Get and retain response. var response = (HttpWebResponse)request.GetResponse(); sendDataResult.HttpWebResponse = response; var responseStream = response.GetResponseStream(); var reader = new StreamReader(responseStream); var resultText = reader.ReadToEnd(); sendDataResult.ResponseText = resultText; sendDataResult.Success = true; } catch (Exception ex) { sendDataResult.ResponseError = ex; sendDataResult.Success = false; } // Return the result of the request. return(sendDataResult); }
async Task <SendDataResult> SendConnectionlessPacketAsync(List <byte> Data) { SendDataResult DataResult = new SendDataResult(); Stopwatch PingWatch = new Stopwatch(); if (SocketInUse) { return(DataResult); } if (SocketClosed) { InitializeClient(); } Log.Debug("SendConnectionlessPacketAsync_Begin"); for (int i = 0; i < 4; ++i) { Data.Insert(0, 0xFF); } SocketInUse = true; Log.Debug("SendConnectionlessPacketAsync_SendAsync_Begin"); AsyncTimer.Start(); PingWatch.Start(); int SentLength = 0; try { SentLength = await Udp.SendAsync(Data.ToArray(), Data.Count); } catch { Log.Debug("SendConnectionlessPacketAsync_SendAsync_Exception"); SocketInUse = false; return(DataResult); } AsyncTimer.Stop(); Log.Debug("SendConnectionlessPacketAsync_SendAsync_End"); if (SentLength == Data.Count) { Log.Debug("SendConnectionlessPacketAsync_ReceiveAsync_Begin"); UdpReceiveResult UdpResult; AsyncTimer.Start(); try { UdpResult = await Udp.ReceiveAsync(); } catch { Log.Debug("SendConnectionlessPacketAsync_ReceiveAsync_Exception"); SocketInUse = false; return(DataResult); } PingWatch.Stop(); AsyncTimer.Stop(); Log.Debug("SendConnectionlessPacketAsync_ReceiveAsync_End"); DataResult.Buffer = UdpResult.Buffer.Skip(4).ToArray(); DataResult.ResponseTime = PingWatch.ElapsedMilliseconds > 9999 ? 9999 : PingWatch.ElapsedMilliseconds; DataResult.Success = true; } SocketInUse = false; Log.Debug("SendConnectionlessPacketAsync_End"); return(DataResult); }