public static XmlDocument RequestXMLData(string requestString, string requestParams)
        {
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(requestString);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

            try {
                HttpResponseMessage message = client.GetAsync(requestParams).Result;
                XmlDocument         doc     = null;
                if (message.IsSuccessStatusCode)
                {
                    Stream stream = message.Content.ReadAsStreamAsync().Result;

                    doc = new XmlDocument();
                    doc.Load(stream);
                }
                return(doc);
            }
            catch (Exception ex) {
                LoggingHelper.LogExceptionInApplicationLog("WebClientHelper.RequestXMLData", new Exception(ex.Message), System.Diagnostics.EventLogEntryType.Error);
                LoggingHelper.WriteExceptionLogEntry("WebClientHelper.RequestXMLData", new Exception(ex.Message));
                return(null);
            }
        }
        public static object RequestJSONData(Type returnType, string requestString, string bearerKey)
        {
            WebClient wc = new WebClient();

            wc.Headers.Add("Accept", "application/json");
            wc.Headers.Add("Content-Type", "multipart/form-data");
            if (bearerKey != null)
            {
                wc.Headers.Add("Authorization", "Bearer " + bearerKey);
            }

            try {
                System.IO.Stream       data   = wc.OpenRead(requestString);
                System.IO.StreamReader reader = new System.IO.StreamReader(data);
                string s = reader.ReadToEnd();
                data.Close();
                reader.Close();

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                WebClientHelper.RegisterJSONConverters(serializer);
                object deserializedObject = serializer.Deserialize(s, returnType);
                return(deserializedObject);
            }
            catch (Exception ex) {
                LoggingHelper.LogExceptionInApplicationLog("WebClientHelper.RequestJSONData", new Exception(ex.Message), System.Diagnostics.EventLogEntryType.Error);
                LoggingHelper.WriteExceptionLogEntry("WebClientHelper.RequestJSONData", new Exception(ex.Message));
                return(null);
            }
        }
        public static void PostJSONData(string requestString, object jsonBody, string bearerKey)
        {
            try {
                WebClient wc = new WebClient();
                wc.Headers.Add("Content-Type", "application/json");
                if (bearerKey != null)
                {
                    wc.Headers.Add("Authorization", "Bearer " + bearerKey);
                }

                JavaScriptSerializer ser = new JavaScriptSerializer();
                wc.UploadString(requestString, ser.Serialize(jsonBody));
            } catch (Exception ex) {
                LoggingHelper.LogExceptionInApplicationLog("WebClientHelper.PostJSONData", new Exception(ex.Message), System.Diagnostics.EventLogEntryType.Error);
                LoggingHelper.WriteExceptionLogEntry("WebClientHelper.PostJSONData", new Exception(ex.Message));
            }
        }
Beispiel #4
0
        private static byte[] SendMessageToSocketAndListenToCallback(IPEndPoint source, IPEndPoint destination, byte[] bufferToSend, BytesReceivedDelegate callBack, ProtocolType protocol, byte[] heartbeatMessage, Regex responseFormat, BytesReceivedDelegate heartbeatCallback, bool isSynchronious, bool useSocketPool)
        {
            Socket TCPSocket = null;

            if (useSocketPool)
            {
                TCPSocket = CheckIfSocketAlreadyOpen(source, destination);
            }
            HeartbeatInfo info = null;

            try {
                if (TCPSocket == null)
                {
                    if (protocol == ProtocolType.Udp)
                    {
                        TCPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, protocol);
                    }
                    else if (protocol == ProtocolType.Tcp)
                    {
                        TCPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, protocol);
                    }

                    TCPSocket.Bind(source);
                    if (protocol == ProtocolType.Tcp)
                    {
                        TCPSocket.Connect(destination);
                        if (useSocketPool)
                        {
                            info = new HeartbeatInfo(TCPSocket, heartbeatMessage, responseFormat, heartbeatCallback, 10);
                            info.CallbackDelegate += new BytesReceivedDelegate(CheckIfHeartbeatFailed);
                            lock (openSocketsList)
                            {
                                openSocketsList.Add(info);
                            }
                        }
                    }
                }
//                lock (TCPSocket) {
                if (bufferToSend != null)
                {
                    if (protocol == ProtocolType.Tcp)
                    {
                        TCPSocket.Send(bufferToSend);
                    }
                    else if (protocol == ProtocolType.Udp)
                    {
                        TCPSocket.SendTo(bufferToSend, destination);
                    }
                }

                StateObject state = new StateObject();
                if (isSynchronious)
                {
                    TCPSocket.ReceiveTimeout = 5000;
                    TCPSocket.Receive(state.buffer);
                    if (!useSocketPool)
                    {
                        TCPSocket.Shutdown(SocketShutdown.Both);
                        TCPSocket.Close();
                    }
                    return(state.buffer);
                }
                else if (callBack != null)
                {
                    state.workSocket        = TCPSocket;
                    state.callbackDelegate += callBack;
                    if (info != null)
                    {
                        state.callbackDelegate += info.ProcessMessageReceivedFromSocket;
                    }
                    TCPSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                           new AsyncCallback(ReadCallback), state);
                }
                //               }
                return(null);
            }
            catch (Exception ex) {
                LoggingHelper.LogExceptionInApplicationLog(ex.Source, ex, EventLogEntryType.Error);
                LoggingHelper.WriteExceptionLogEntry(ex.Source, ex);

                if (TCPSocket != null && TCPSocket.Connected)
                {
                    TCPSocket.Shutdown(SocketShutdown.Both);
                    TCPSocket.Close();
                }
            }
            return(null);
        }