Exemplo n.º 1
0
        public static string SendHttpGet(WebRequest request, YahooMessengerAuthentication authentication)
        {
            request.Method = "GET";

            if(authentication!=null)
            {
                if(!authentication.IsUsingOAuth)
                    request.Headers.Add("Cookie",authentication.Cookie);
                else request.Headers.Add("Authorization", "OAuth");
            }

            using(var response = (HttpWebResponse)request.GetResponse())
            {
                if(response == null) throw new NullReferenceException();

                var respCode = response.StatusCode;

                if (respCode != HttpStatusCode.OK)
                    throw new HttpException(HttpException.HTTP_OK_NOT_RECEIVED, respCode.ToString());

                var dataStream = response.GetResponseStream();

                if (dataStream == null) throw new NullReferenceException();
                var reader = new StreamReader(dataStream);

                // Read the content.
                var responseFromServer = new StringBuilder("");
                responseFromServer.Append(reader.ReadToEnd());
                reader.Close();
                dataStream.Close();
                response.Close();

                return responseFromServer.ToString();
            }
        }
Exemplo n.º 2
0
        public static string SendHttpPost(WebRequest request, YahooMessengerAuthentication authentication, string content)
        {
            request.Method = "POST";
            byte[] byteArray = Encoding.UTF8.GetBytes(content);
            request.ContentLength = byteArray.Length;
            var dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            using(var response = (HttpWebResponse) request.GetResponse())
            {
                if (response == null) throw new NullReferenceException();

                var respCode = response.StatusCode;

                if (respCode != HttpStatusCode.OK)
                    throw new HttpException(HttpException.HTTP_OK_NOT_RECEIVED, respCode.ToString());

                dataStream = response.GetResponseStream();
                if (dataStream == null) throw new NullReferenceException();
                var reader = new StreamReader(dataStream);

                var responseFromServer = new StringBuilder("");
                responseFromServer.Append(reader.ReadToEnd());
                reader.Close();
                dataStream.Close();
                response.Close();

                return responseFromServer.ToString();
            }
        }
        public void ExecuteRequest()
        {
            //  Verify mandatory parameters
            if (ConsumerKey == null)
                throw new MessengerException(MessengerException.NO_CONSUMER_KEY_GIVEN, "No consumer key given");

            AuthenticationToken = new YahooMessengerAuthentication()
                                      {
                                          OauthConsumerKey = ConsumerKey
                                      };

            //  Create the HTTP URL
            string url = YahooMessengerConstants.ExchangePartGetUrl + "?" +
                    "&oauth_consumer_key=" + ConsumerKey +
                    "&oauth_signature_method=" + AuthenticationToken.OauthSignatureMethod +
                    "&oauth_nonce=" + AuthenticationToken.OauthNonce +
                    "&oauth_timestamp=" + AuthenticationToken.OauthTimestamp +
                    "&oauth_signature=" + Signature +
                    "&oauth_version=" + AuthenticationToken.OauthVersion +
                    "&oauth_token=" + Token;

            //  Perform the actual call to the server
            WebRequest request = WebRequest.Create(url);
            var resultStr = HttpUtils.SendHttpGet(request);
            var tmp = HttpUtility.ParseQueryString(resultStr);
            Token = tmp["oauth_token"];
            TokenSecret = tmp["oauth_token_secret"];

            AuthenticationToken.OauthToken = Token;
            AuthenticationToken.OauthSignature = ConsummerSecret + "%26" + TokenSecret;
        }