public override void Send(byte[] outgoingData, object userState)
        {
            if (opened || (null != (outgoingData = GetOpenOrClosePacket(outgoingData))))
            {
                if (isWebRequestInProcess)
                {
                    lock (SyncObject)
                    {
                        sendingQueue.Enqueue(new QueueState(outgoingData, userState));
                        action = HttpPollAction.None;
                    }
                }
                else
                {
                    isWebRequestInProcess = true;
                    HttpPollAction oldAction = action;

                    lock (SyncObject)
                    {
                        action = HttpPollAction.None;

                        if (pollTimer.Enabled)
                        {
                            pollTimer.Stop();
                        }
                    }

                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(GenerateURI(oldAction));
                    ConnectivitySettings.SetupWebRequest(request);
                    request.Accept                    = "*/*";
                    request.Method                    = "POST";
                    request.KeepAlive                 = true;
                    request.AllowAutoRedirect         = false;
                    request.AllowWriteStreamBuffering = false;
                    request.ContentLength             = outgoingData.Length;
                    request.Headers.Add("Pragma", "no-cache");

                    // Set timeouts (10+40=50) = PNG average interval
                    request.Timeout          = 10000; // 10 seconds for GetRequestStream(). Timeout error occurs when the server is busy.
                    request.ReadWriteTimeout = 40000; // 40 seconds for Read()/Write(). PNG interval is 45-60, so timeout must be <= 40.

                    // Bypass msnp blockers
                    request.ContentType = "text/html; charset=UTF-8";
                    request.UserAgent   = @"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.65 Safari/535.11";
                    request.Headers.Add("X-Requested-Session-Content-Type", "text/html");

                    // Enable GZIP
                    request.AutomaticDecompression = Settings.EnableGzipCompressionForWebServices ? DecompressionMethods.GZip : DecompressionMethods.None;

                    // Don't block current thread.
                    HttpState httpState = new HttpState(request, outgoingData, userState, oldAction);
                    request.BeginGetRequestStream(EndGetRequestStreamCallback, httpState);
                }
            }
        }
        private static void DoDownload(object param)
        {
            object[] paramlist = param as object[];

            string usertileURL = paramlist[0].ToString();
            EventHandler <ObjectEventArgs> callBack             = paramlist[1] as EventHandler <ObjectEventArgs>;
            ConnectivitySettings           connectivitySettings = paramlist[2] as ConnectivitySettings;

            try
            {
                Uri uri = new Uri(usertileURL);

                HttpWebRequest fwr = (HttpWebRequest)WebRequest.Create(uri);
                if (connectivitySettings != null)
                {
                    connectivitySettings.SetupWebRequest(fwr);
                }

                fwr.Timeout = 10000;
                fwr.BeginGetResponse(delegate(IAsyncResult result)
                {
                    try
                    {
                        Stream stream   = ((WebRequest)result.AsyncState).EndGetResponse(result).GetResponseStream();
                        MemoryStream ms = new MemoryStream();
                        byte[] data     = new byte[8192];
                        int read;
                        while ((read = stream.Read(data, 0, data.Length)) > 0)
                        {
                            ms.Write(data, 0, read);
                        }
                        stream.Close();

                        if (callBack != null)
                        {
                            callBack(fwr, new ObjectEventArgs(ms.ToArray()));
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLineIf(Settings.TraceSwitch.TraceError, "[HttpDataDownloader] Error: " + ex.Message);
                    }
                }, fwr);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("[HttpDataDownloader] Error: " + ex.Message);
            }
        }
        internal static entryType UpdatePersonalStatus(string newDisplayName, long ownerCID, string authToken, ConnectivitySettings connectivitySettings)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(liveAPIBaseURL + "/Users({0})/Status".Replace("{0}", ownerCID.ToString()));

            if (connectivitySettings != null)
            {
                connectivitySettings.SetupWebRequest(request);
            }
            request.Method      = "POST";
            request.Accept      = @"application/atom+xml; type=entry";
            request.ContentType = @"application/atom+xml";
            request.Headers.Add(HttpRequestHeader.Authorization, "WLID1.0 " + authToken);
            request.Headers.Add("AppId", appID);

            entryType entry = new entryType();

            if (!string.IsNullOrEmpty(newDisplayName))
            {
                entry.title       = new entryTypeTitle();
                entry.title.Value = newDisplayName;
            }

            XmlSerializer ser       = new XmlSerializer(typeof(entryType));
            MemoryStream  memStream = new MemoryStream();

            ser.Serialize(memStream, entry);
            string xmlString = Encoding.UTF8.GetString(memStream.ToArray());
            int    xmlLength = Encoding.UTF8.GetByteCount(xmlString);

            request.ContentLength = xmlLength;
            request.GetRequestStream().Write(Encoding.UTF8.GetBytes(xmlString), 0, xmlLength);

            XmlReaderSettings readerSettings = new XmlReaderSettings();

            readerSettings.CloseInput = true;

            using (XmlReader xmlReader = XmlReader.Create(request.GetResponse().GetResponseStream(), readerSettings))
            {
                return((entryType)ser.Deserialize(xmlReader));
            }
        }
Example #4
0
        internal static entryType UpdatePersonalStatus(string newDisplayName, long ownerCID, string authToken, ConnectivitySettings connectivitySettings)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(liveAPIBaseURL + "/Users({0})/Status".Replace("{0}", ownerCID.ToString()));
            if (connectivitySettings != null)
            {
                connectivitySettings.SetupWebRequest(request);
            }
            request.Method = "POST";
            request.Accept = @"application/atom+xml; type=entry";
            request.ContentType = @"application/atom+xml";
            request.Headers.Add(HttpRequestHeader.Authorization, "WLID1.0 " + authToken);
            request.Headers.Add("AppId", appID);

            entryType entry = new entryType();

            if (!string.IsNullOrEmpty(newDisplayName))
            {
                entry.title = new entryTypeTitle();
                entry.title.Value = newDisplayName;
            }

            XmlSerializer ser = new XmlSerializer(typeof(entryType));
            MemoryStream memStream = new MemoryStream();
            ser.Serialize(memStream, entry);
            string xmlString = Encoding.UTF8.GetString(memStream.ToArray());
            int xmlLength = Encoding.UTF8.GetByteCount(xmlString);

            request.ContentLength = xmlLength;
            request.GetRequestStream().Write(Encoding.UTF8.GetBytes(xmlString), 0, xmlLength);

            XmlReaderSettings readerSettings = new XmlReaderSettings();
            readerSettings.CloseInput = true;

            using (XmlReader xmlReader = XmlReader.Create(request.GetResponse().GetResponseStream(), readerSettings))
            {
                return (entryType)ser.Deserialize(xmlReader);
            }
        }