Exemplo n.º 1
0
        public void RequestPacket(PacketMediaUpload packet, ClientSendTweet tweet, Action <ClientSendTweet, ClientMultimedia> callback)
        {
            Task t = Task.Factory.StartNew(new Action((() => TaskRequestPacket(packet, tweet, callback))), token);

            t.ContinueWith(TaskComplete);
            listTask.TryAdd(t);
        }
Exemplo n.º 2
0
        public void SendMedia(ClientSendTweet tweet)
        {
            if (string.IsNullOrEmpty(tweet.multiPath))
            {
                BitmapImage       image   = tweet.GetNextImage();
                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(image));
                PacketMediaUpload packet = new PacketMediaUpload();
                encoder.Save(packet.mediaStream);

                packet.extension = encoder.CodecInfo.FileExtensions;
                packet.eresponse = eResponse.IMAGE;

                RequestPacket(packet, tweet, responseInstence.Multimedia);
            }
            else
            {
                PacketMediaUpload packet = new PacketMediaUpload();
                using (var stream = File.OpenRead(tweet.multiPath))
                    stream.CopyTo(packet.mediaStream);

                packet.extension = Path.GetExtension(tweet.multiPath);
                packet.eresponse = eResponse.IMAGE;
                RequestPacket(packet, tweet, responseInstence.Multimedia);
            }
        }
Exemplo n.º 3
0
 private void TaskRequestPacket(PacketMediaUpload packet, ClientSendTweet tweet, Action <ClientSendTweet, ClientMultimedia> callback)
 {
     WebInstence.SendMultimedia(packet, tweet, callback);
 }
Exemplo n.º 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);
        }
Exemplo n.º 5
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();
            }
        }