public static string TransmitExtended(string address) { OnDataSend?.Invoke(address, null); try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address); request.UserAgent = AppSettings.UserAgent; request.Accept = "application/json"; WebResponse response = (HttpWebResponse)request.GetResponse(); if (response == null) { return(null); } Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); string strResponse = reader.ReadToEnd(); OnDataReceived?.Invoke(strResponse); stream.Close(); reader.Close(); response.Close(); return(strResponse); } catch (Exception e) { OnDataErrorReceived?.Invoke(e.Message); return(null); } }
/// <summary> /// Gets the CSV file as a stream reader object. /// </summary> /// <param name="address">The URI to use</param> /// <returns>The response from Server</returns> public static StreamReader GetCsvStream(string address) { OnDataSend?.Invoke(address, null); try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address); request.UserAgent = AppSettings.UserAgent; request.Accept = "text/csv"; var response = (HttpWebResponse)request.GetResponse(); if (response == null) { return(null); } Stream stream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(stream); OnDataReceived?.Invoke(response.StatusCode.ToString()); return(streamReader); } catch (Exception e) { OnDataErrorReceived?.Invoke(e.Message); return(null); } }
/// <summary> /// Sends the specified text to FICS. /// </summary> /// <param name="text">The text to send.</param> public void Send(string text) { var byteDataToSend = Encoding.ASCII.GetBytes(text + FICSConstants.EndOfLine); _socket.BeginSend(byteDataToSend, 0, byteDataToSend.Length, 0, SendCallback, _socket); OnDataSend?.Invoke(this, new DataSentEventArgs(text)); }
/// <summary> /// Communicates to and from a Server /// </summary> /// <param name="address">The URI to use</param> /// <param name="data">The Data to send</param> /// <param name="logResponse">Shall we log the response?</param> /// <returns>The response from Server</returns> public static string Transmit(string address, string data, bool logResponse = true) { OnDataSend?.Invoke(address, data); try { ServicePointManager.Expect100Continue = false; var client = new ExtendedWebClient { Timeout = 120000, Encoding = Encoding.UTF8 }; client.Headers.Add("user-agent", AppSettings.UserAgent); var response = string.Empty; if (string.IsNullOrEmpty(data)) { response = client.DownloadString(address); } else { response = client.UploadString(address, data); } if (logResponse && OnDataReceived != null) { OnDataReceived(response); } return(response); } catch (WebException we) { string ret = null; // something bad happened var response = we.Response as HttpWebResponse; try { using (var stream = response.GetResponseStream()) { using (var reader = new StreamReader(stream)) { ret = reader.ReadToEnd(); OnDataErrorReceived?.Invoke(ret); } } } catch (Exception e) { OnDataErrorReceived?.Invoke(e.Message); } return(ret); } }
/// <summary> /// 消息发送任务 /// </summary> /// <param name="state"></param> private void MessageCallBack(object state) { if (messageArray.Count > 0) { SendDataInfo info = messageArray.FirstOrDefault(); if (null != info) { OnDataSend.Invoke(info); } messageArray.Remove(info); } }
public static string GetFromTrakt(string address, out WebHeaderCollection headerCollection, string method = "GET") { headerCollection = new WebHeaderCollection(); OnDataSend?.Invoke(address, null); var request = WebRequest.Create(address) as HttpWebRequest; request.KeepAlive = true; request.Method = method; request.ContentLength = 0; request.Timeout = 120000; request.ContentType = "application/json"; request.UserAgent = AppSettings.UserAgent; foreach (var header in CustomRequestHeaders) { request.Headers.Add(header.Key, header.Value); } try { var response = request.GetResponse() as HttpWebResponse; if (response == null) { return(null); } Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); string strResponse = reader.ReadToEnd(); headerCollection = response.Headers; OnDataReceived?.Invoke(string.IsNullOrEmpty(strResponse) ? response.StatusCode.ToString() : strResponse); stream.Close(); reader.Close(); response.Close(); return(strResponse); } catch (WebException e) { OnDataErrorReceived?.Invoke(e.Message); return(null); } }
/// <summary> /// Will open a NetworkStream and send data to the simulator. This method will /// serialize data recieved from the GUI controls and then Invoke the /// DataSend event which will append the Sent Data controls in the form. /// </summary> public void SendDataToSimulator() { // Create an empty ControlsUpdate struct. // Get the incoming stream from the NetworkClient. NetworkStream networkStream = networkClient.GetStream(); JavaScriptSerializer serializer = new JavaScriptSerializer(); // Serialize the data into JSON format and encode it into a byte buffer. string dataToSend = serializer.Serialize(controlsData); byte[] bytesToSend = Encoding.ASCII.GetBytes(dataToSend); // Send the data to the simulator using the NetworkStream. networkStream.Write(bytesToSend, 0, bytesToSend.Length); // Invoke the data send event. OnDataSend?.Invoke(controlsData); }
protected void CallOnDataSend(Robot_base connection, byte[] data, eventStatus status = eventStatus.any) { OnDataSend?.Invoke(this, connection, data, status); }
static string GetFromTmdb(string address, int delayRequest = 0) { if (delayRequest > 0) { Thread.Sleep(1000 + delayRequest); } OnDataSend?.Invoke(address, null); var headerCollection = new WebHeaderCollection(); var request = WebRequest.Create(address) as HttpWebRequest; request.KeepAlive = true; request.Method = "GET"; request.ContentLength = 0; request.Timeout = 120000; request.ContentType = "application/json"; request.UserAgent = UserAgent; string strResponse = null; try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response == null) { return(null); } Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); strResponse = reader.ReadToEnd(); headerCollection = response.Headers; OnDataReceived?.Invoke(strResponse, response); stream.Close(); reader.Close(); response.Close(); } catch (WebException wex) { string errorMessage = wex.Message; if (wex.Status == WebExceptionStatus.ProtocolError) { var response = wex.Response as HttpWebResponse; string headers = string.Empty; foreach (string key in response.Headers.AllKeys) { headers += string.Format("{0}: {1}, ", key, response.Headers[key]); } errorMessage = string.Format("Protocol Error, Code = '{0}', Description = '{1}', Url = '{2}', Headers = '{3}'", (int)response.StatusCode, response.StatusDescription, address, headers.TrimEnd(new char[] { ',', ' ' })); // check if we got a 429 error code // https://developers.themoviedb.org/3/getting-started/request-rate-limiting if ((int)response.StatusCode == 429) { int retry = 0; int.TryParse(response.Headers["Retry-After"], out retry); errorMessage = string.Format("Request Rate Limiting is in effect, retrying request in {0} seconds. Url = '{1}'", retry, address); OnDataError?.Invoke(errorMessage); return(GetFromTmdb(address, retry * 1000)); } } OnDataError?.Invoke(errorMessage); strResponse = null; } catch (IOException ioe) { string errorMessage = string.Format("Request failed due to an IO error, Description = '{0}', Url = '{1}', Method = 'GET'", ioe.Message, address); OnDataError?.Invoke(ioe.Message); strResponse = null; } return(strResponse); }
/// <summary> /// RaiseDataSend /// </summary> /// <param name="receive"></param> protected void RaiseDataSend(byte[] receive) { OnDataSend?.Invoke(this, receive); }
private void DataSend(object sender, DataSendEventArgs args) { OnDataSend?.Invoke(sender, args); }
static string GetFromFanartTv(string address) { OnDataSend?.Invoke(address, null); var headerCollection = new WebHeaderCollection(); var request = WebRequest.Create(address) as HttpWebRequest; request.KeepAlive = true; request.Method = "GET"; request.ContentLength = 0; request.Timeout = 120000; request.ContentType = "application/json"; request.UserAgent = UserAgent; // add required headers for authorisation request.Headers.Add("api-key", ApiKey); if (!string.IsNullOrEmpty(ClientKey)) { request.Headers.Add("client_key", ClientKey); } string strResponse = null; try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response == null) { return(null); } Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); strResponse = reader.ReadToEnd(); headerCollection = response.Headers; OnDataReceived?.Invoke(strResponse, response); stream.Close(); reader.Close(); response.Close(); } catch (WebException wex) { string errorMessage = wex.Message; if (wex.Status == WebExceptionStatus.ProtocolError) { var response = wex.Response as HttpWebResponse; string headers = string.Empty; foreach (string key in response.Headers.AllKeys) { headers += string.Format("{0}: {1}, ", key, response.Headers[key]); } errorMessage = string.Format("Protocol Error, Code = '{0}', Description = '{1}', Url = '{2}', Headers = '{3}'", (int)response.StatusCode, response.StatusDescription, address, headers.TrimEnd(new char[] { ',', ' ' })); } OnDataError?.Invoke(errorMessage); strResponse = null; } catch (IOException ioe) { string errorMessage = string.Format("Request failed due to an IO error, Description = '{0}', Url = '{1}', Method = 'GET'", ioe.Message, address); OnDataError?.Invoke(ioe.Message); strResponse = null; } return(strResponse); }
/// <summary> /// Virtual handler for any action to be taken when a device is removed. Override to use. /// </summary> protected virtual void HandleDataSend(HIDReport report) { OnDataSend?.Invoke(this, report); }
protected void CallOnDataSend(NetConnection connection, byte[] data) { OnDataSend?.Invoke(this, connection, data); }