Beispiel #1
0
        //public void SetUserTokenSecret(string secret)
        //{
        //	userInfo.UpdateSecretToken(secret);
        //}

        //public void SetUserToken(string token)
        //{
        //	userInfo.UpdateToken(token);
        //	OAuth.GetInstence().OAuthToken = token;
        //}

        public void ClearToken()
        {
            userInfo.Clear();
            OAuth.GetInstence().Clear();
        }
Beispiel #2
0
 public void UpdateToken(Web.ResOAuth oauth)
 {
     userInfo.UpdateToken(oauth);
     OAuth.GetInstence().OAuthToken = oauth.tokenStr;
 }
Beispiel #3
0
        public string SyncRequest(BasePacket parameter)
        {
            string         ret = string.Empty;
            HttpWebRequest req;

            if (parameter.method == "POST")
            {
                req = (HttpWebRequest)WebRequest.Create(parameter.url);
            }
            else//GET일경우
            {
                req = (HttpWebRequest)WebRequest.Create(parameter.MethodGetUrl());
            }
            TwitterRequest request = new TwitterRequest(req, parameter);

            req.ContentType = "application/x-www-form-urlencoded;encoding=utf-8";
            req.Method      = parameter.method;
            req.Headers.Add("Authorization", OAuth.GetInstence().GetHeader(parameter));



            if (parameter.dicParams.Count > 0 && parameter.method == "POST")//POST일 때에만 Stream사용
            {
                //-----------------------------------------------------------------------------------
                //------------------------------------Send------------------------------------------
                //-----------------------------------------------------------------------------------
                try//send!
                {
                    Stream        stream = req.GetRequestStream();
                    StringBuilder sb     = new StringBuilder();

                    foreach (string item in parameter.dicParams.Keys)
                    {
                        if (parameter.dicParams[item] != "")
                        {
                            sb.Append(item);
                            sb.Append("=");
                            OAuth.GetInstence().CalcParamUri(sb, parameter.dicParams[item]);
                            sb.Append("&");
                        }
                    }
                    string sendData = sb.ToString();
                    byte[] bytes    = Encoding.UTF8.GetBytes(sendData);

                    stream.Write(bytes, 0, sendData.Length);
                    stream.Close();
                }
                catch (WebException e)
                {
                    using (Stream stream2 = e.Response?.GetResponseStream())
                    {
                        if (stream2 == null)
                        {
                            return(string.Empty);
                        }
                        StreamReader srReadData = new StreamReader(stream2, Encoding.Default);
                        string       log        = srReadData.ReadToEnd();
                        if (OnResponseError != null)
                        {
                            Application.Current.Dispatcher.BeginInvoke(OnResponseError, new object[] { log });
                        }
                    }
                }
                catch (Exception e)
                {
                    App.SendException(e);
                }
            }

            //-----------------------------------------------------------------------------------
            //-------------------------------Response------------------------------------------
            //-----------------------------------------------------------------------------------

            try//Response!!!
            {
                using (WebResponse response = req.GetResponse())
                    using (Stream stream = response.GetResponseStream())
                        using (StreamReader streamRead = new StreamReader(stream))
                            ret = streamRead.ReadToEnd();
            }
            catch (WebException e)
            {
                //using (Stream stream = e.Response.GetResponseStream())
                //{
                //	StreamReader srReadData = new StreamReader(stream, Encoding.Default);
                //	string log = srReadData.ReadToEnd();
                //	DalsaeInstence.ResponseError(log);
                //}
            }
            catch (Exception e)
            {
                App.SendException(e);
            }

            return(ret);
        }
Beispiel #4
0
        //이미지 업로드 시 요청하는 함수, 동기 전송
        //obj: 이미지 bytes를 담고있는 파라메터
        public string SendMultimedia2(PacketMediaUpload parameter)
        {
            string         ret     = string.Empty;
            HttpWebRequest req     = (HttpWebRequest)WebRequest.Create(parameter.url);
            TwitterRequest request = new TwitterRequest(req, parameter);

            var boundary = Guid.NewGuid().ToString().Replace("-", string.Empty);

            req.ContentType = "multipart/form-data;encoding=utf-8;boundary=" + boundary;
            req.Method      = parameter.method;
            req.Headers.Add("Authorization", OAuth.GetInstence().GetHeader(parameter));

            boundary = "--" + boundary;

            //-----------------------------------------------------------------------------------
            //------------------------------------Send------------------------------------------
            //-----------------------------------------------------------------------------------
            try//send!
            {
                using (Stream stream = req.GetRequestStream())
                    using (var writer = new StreamWriter(stream, Encoding.UTF8))
                    {
                        writer.NewLine = "\r\n";
                        writer.WriteLine(boundary);
                        writer.WriteLine("Content-Type: application/octet-stream");
                        writer.WriteLine($"Content-Disposition: form-data; name=\"media\"; filename=\"img{parameter.extension}\"");
                        writer.WriteLine();
                        writer.Flush();

                        parameter.mediaStream.Position = 0;
                        parameter.mediaStream.CopyTo(stream);

                        writer.WriteLine();
                        writer.WriteLine(boundary + "--");
                        writer.Flush();
                    }
            }
            catch (WebException e)
            {
                using (Stream stream = e.Response?.GetResponseStream())
                {
                    if (stream == null)
                    {
                        return(string.Empty);
                    }
                    using (StreamReader srReadData = new StreamReader(stream, Encoding.Default))
                    {
                        string log = srReadData.ReadToEnd();
                        if (OnResponseError != null)
                        {
                            Application.Current.Dispatcher.BeginInvoke(OnResponseError, new object[] { log });
                        }
                    }
                }
            }
            //-----------------------------------------------------------------------------------
            //-------------------------------Response------------------------------------------
            //-----------------------------------------------------------------------------------

            try//Response!!!
            {
                using (WebResponse response = req.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader streamRead = new StreamReader(stream))
                            ret = streamRead.ReadToEnd();
                    }
                }
            }
            catch (WebException e)
            {
                using (Stream stream = e.Response?.GetResponseStream())
                {
                    if (stream == null)
                    {
                        return(string.Empty);
                    }
                    using (StreamReader srReadData = new StreamReader(stream, Encoding.Default))
                    {
                        string log = srReadData.ReadToEnd();
                        if (OnResponseError != null)
                        {
                            Application.Current.Dispatcher.BeginInvoke(OnResponseError, new object[] { log });
                        }
                    }
                }
            }

            return(ret);
        }
Beispiel #5
0
        //public void DisconnectingUserStreaming()
        //{
        //	userStream.Disconnecting();
        //	userStream = new UserStream();
        //	DalsaeInstence.ConnectedStreaming(false);
        //}
        //public bool isConnectedUserStreaming() { return userStream.isConnectedStreaming; }

        //public void ConnectUserStream(object obj)
        //{
        //	if (obj == null) return;
        //	userStream.ConnectStreaming(obj as BasePacket);
        //}


        public void SyncRequest <TRes>(BasePacket packet, UIProperty property, Action <TRes, UIProperty> callback)
        {
            HttpWebRequest req;

            if (packet.method == "POST")
            {
                req = (HttpWebRequest)WebRequest.Create(packet.url);
            }
            else            //GET일경우
            {
                req = (HttpWebRequest)WebRequest.Create(packet.MethodGetUrl());
            }
            TwitterRequest request = new TwitterRequest(req, packet);

            req.ContentType = "application/x-www-form-urlencoded;encoding=utf-8";
            req.Method      = packet.method;
            req.Headers.Add("Authorization", OAuth.GetInstence().GetHeader(packet));



            if (packet.dicParams.Count > 0 && packet.method == "POST")            //POST일 때에만 Stream사용
            {
                //-----------------------------------------------------------------------------------
                //------------------------------------Send------------------------------------------
                //-----------------------------------------------------------------------------------
                try                //send!
                {
                    Send(packet, req);
                }
                catch (WebException e)
                {
                    using (Stream stream2 = e.Response?.GetResponseStream())
                    {
                        StreamReader srReadData = new StreamReader(stream2, Encoding.Default);
                        string       log        = srReadData.ReadToEnd();
                        if (OnResponseError != null)
                        {
                            Application.Current.Dispatcher.BeginInvoke(OnResponseError, new object[] { log });
                        }
                    }
                }
                catch (Exception e)
                {
                    OnResponseError?.Invoke(packet, null);
                }
            }

            try            //Response!!!
            {
                string json = Recv(packet, req);
                TRes   ret  = default(TRes);
                if (json?.Length > 0)
                {
                    ret = JsonConvert.DeserializeObject <TRes>(json);
                }
                if (callback != null)
                {
                    Application.Current.Dispatcher.BeginInvoke(callback, new object[] { ret, property });
                }
            }
            catch (WebException e)
            {
                if (e.Message.IndexOf("408") > -1)                //timeout시 재 전송
                {
                    Manager.APICallAgent.apiInstence.RequestSingleTweetPacket <TRes>(packet, property, callback);
                }
                else if (e.Response == null)
                {
                    App.SendException(e);
                }
                else
                {
                    //ClientAPIError error = null;
                    using (Stream stream = e.Response?.GetResponseStream())
                        using (StreamReader srReadData = new StreamReader(stream, Encoding.Default))
                        {
                            string log = srReadData.ReadToEnd();
                            if (string.IsNullOrEmpty(log) == false)
                            {
                                if (OnResponseError != null)
                                {
                                    Application.Current.Dispatcher.BeginInvoke(OnResponseError, new object[] { packet, log });
                                }
                            }
                        }
                }
            }
            catch (Exception e)
            {
                OnResponseError?.Invoke(packet, null);
                App.SendException(e);
            }
        }
Beispiel #6
0
        public void SendMultimedia(PacketMediaUpload packet, ClientSendTweet tweet, Action <ClientSendTweet, ClientMultimedia> callback)
        {
            HttpWebRequest req     = (HttpWebRequest)WebRequest.Create(packet.url);
            TwitterRequest request = new TwitterRequest(req, packet);

            var boundary = Guid.NewGuid().ToString().Replace("-", string.Empty);

            req.ContentType = "multipart/form-data;encoding=utf-8;boundary=" + boundary;
            req.Method      = packet.method;
            req.Headers.Add("Authorization", OAuth.GetInstence().GetHeader(packet));

            boundary = "--" + boundary;

            try            //send!
            {
                using (Stream stream = req.GetRequestStream())
                    using (var writer = new StreamWriter(stream, Encoding.UTF8))
                    {
                        writer.NewLine = "\r\n";
                        writer.WriteLine(boundary);
                        writer.WriteLine("Content-Type: application/octet-stream");
                        writer.WriteLine($"Content-Disposition: form-data; name=\"media\"; filename=\"img{packet.extension}\"");
                        writer.WriteLine();
                        writer.Flush();

                        packet.mediaStream.Position = 0;
                        packet.mediaStream.CopyTo(stream);

                        writer.WriteLine();
                        writer.WriteLine(boundary + "--");
                        writer.Flush();
                    }
            }
            catch (WebException e)
            {
                using (Stream stream = e.Response?.GetResponseStream())
                {
                    if (stream == null)
                    {
                        return;
                    }
                    using (StreamReader srReadData = new StreamReader(stream, Encoding.Default))
                    {
                        string log = srReadData.ReadToEnd();
                        if (OnResponseError != null)
                        {
                            Application.Current.Dispatcher.BeginInvoke(OnResponseError, new object[] { packet, log });
                        }
                    }
                }
            }
            catch (Exception e)
            {
                packet.Dispose();
                App.SendException(e);
                return;
            }

            try            //Response!!!
            {
                using (WebResponse response = req.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        if (stream == null)
                        {
                            return;
                        }
                        using (StreamReader streamRead = new StreamReader(stream))
                        {
                            string           json  = streamRead.ReadToEnd();
                            ClientMultimedia media = JsonConvert.DeserializeObject <ClientMultimedia>(json);
                            callback?.Invoke(tweet, media);
                        }
                    }
                }
            }
            catch (WebException e)
            {
                using (Stream stream = e.Response?.GetResponseStream())
                {
                    if (stream == null)
                    {
                        return;
                    }
                    using (StreamReader srReadData = new StreamReader(stream, Encoding.Default))
                    {
                        string log = srReadData.ReadToEnd();
                        if (OnResponseError != null)
                        {
                            Application.Current.Dispatcher.BeginInvoke(OnResponseError, new object[] { log });
                        }
                    }
                }
            }
            catch (Exception e)
            {
                App.SendException(e);
            }
            finally
            {
                packet.Dispose();
            }
        }