Пример #1
0
            /// <summary>
            /// WebRequestWithPut
            /// </summary>
            /// <param name="method">WebRequestWithPut</param>
            /// <param name="url"></param>
            /// <param name="postData"></param>
            /// <returns></returns>
            public string APIWebRequest(Web.Method method, string url, string postData)
            {
                Uri    uri       = new Uri(url);
                string nonce     = this.GenerateNonce();
                string timeStamp = this.GenerateTimeStamp();

                string outUrl, querystring;

                //Generate Signature
                string sig = this.GenerateSignature(uri,
                                                    this.ConsumerKey,
                                                    this.ConsumerSecret,
                                                    this.Token,
                                                    this.TokenSecret,
                                                    method.ToString(),
                                                    timeStamp,
                                                    nonce,
                                                    out outUrl,
                                                    out querystring);

                //querystring += "&oauth_signature=" + HttpUtility.UrlEncode(sig);
                //NameValueCollection qs = HttpUtility.ParseQueryString(querystring);

                //string finalGetUrl = outUrl + "?" + querystring;

                HttpWebRequest webRequest = null;

                //webRequest = System.Net.WebRequest.Create(finalGetUrl) as HttpWebRequest;
                webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
                //webRequest.ContentType = "text/xml";
                webRequest.Method      = method.ToString();
                webRequest.Credentials = CredentialCache.DefaultCredentials;
                webRequest.AllowWriteStreamBuffering = true;

                webRequest.PreAuthenticate = true;
                webRequest.ServicePoint.Expect100Continue = false;
                ServicePointManager.SecurityProtocol      = SecurityProtocolType.Ssl3;

                webRequest.Headers.Add("Authorization", "OAuth realm=\"http://api.linkedin.com/\",oauth_consumer_key=\"" + this.ConsumerKey + "\",oauth_token=\"" + this.Token + "\",oauth_signature_method=\"HMAC-SHA1\",oauth_signature=\"" + HttpUtility.UrlEncode(sig) + "\",oauth_timestamp=\"" + timeStamp + "\",oauth_nonce=\"" + nonce + "\",oauth_verifier=\"" + this.Verifier + "\", oauth_version=\"1.0\"");
                //webRequest.Headers.Add("Authorization", "OAuth oauth_nonce=\"" + nonce + "\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"" + timeStamp + "\", oauth_consumer_key=\"" + this.ConsumerKey + "\", oauth_token=\"" + this.Token + "\", oauth_signature=\"" + HttpUtility.UrlEncode(sig) + "\", oauth_version=\"1.0\"");
                if (postData != null)
                {
                    byte[] fileToSend = Encoding.UTF8.GetBytes(postData);
                    webRequest.ContentLength = fileToSend.Length;

                    Stream reqStream = webRequest.GetRequestStream();

                    reqStream.Write(fileToSend, 0, fileToSend.Length);
                    reqStream.Close();
                }

                string returned = Web.WebResponseGet(webRequest);

                return(returned);
            }
Пример #2
0
            /// <summary>
            /// Web Request Wrapper
            /// </summary>
            /// <param name="method">Http Method</param>
            /// <param name="url">Full url to the web resource</param>
            /// <param name="postData">Data to post in querystring format</param>
            /// <returns>The web server response.</returns>
            public string WebRequest(Web.Method method, string url, string postData)
            {
                HttpWebRequest webRequest    = null;
                StreamWriter   requestWriter = null;
                string         responseData  = "";

                webRequest        = System.Net.WebRequest.Create(url) as HttpWebRequest;
                webRequest.Method = method.ToString();
                webRequest.ServicePoint.Expect100Continue = false;
                //webRequest.UserAgent  = "Identify your application please.";
                //webRequest.Timeout = 20000;

                if (method == Web.Method.POST || method == Web.Method.DELETE)
                {
                    webRequest.ContentType = "application/x-www-form-urlencoded";

                    //POST the data.
                    requestWriter = new StreamWriter(webRequest.GetRequestStream());
                    try
                    {
                        requestWriter.Write(postData);
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        requestWriter.Close();
                        requestWriter = null;
                    }
                }

                responseData = WebResponseGet(webRequest);

                webRequest = null;

                return(responseData);
            }
Пример #3
0
            /// <summary>
            /// Submit a web request using oAuth.
            /// </summary>
            /// <param name="method">GET or POST</param>
            /// <param name="url">The full url, including the querystring.</param>
            /// <param name="postData">Data to post (querystring format)</param>
            /// <returns>The web server response.</returns>
            public string oAuthWebRequest(Web.Method method, string url, string postData)
            {
                string outUrl      = "";
                string querystring = "";
                string ret         = "";


                //Setup postData for signing.
                //Add the postData to the querystring.
                if (method == Web.Method.POST || method == Web.Method.DELETE)
                {
                    if (postData.Length > 0)
                    {
                        //Decode the parameters and re-encode using the oAuth UrlEncode method.
                        NameValueCollection qs = HttpUtility.ParseQueryString(postData);
                        postData = "";
                        foreach (string key in qs.AllKeys)
                        {
                            if (postData.Length > 0)
                            {
                                postData += "&";
                            }
                            qs[key]   = HttpUtility.UrlDecode(qs[key]);
                            qs[key]   = this.UrlEncode(qs[key]);
                            postData += key + "=" + qs[key];
                        }
                        if (url.IndexOf("?") > 0)
                        {
                            url += "&";
                        }
                        else
                        {
                            url += "?";
                        }
                        url += postData;
                    }
                }

                Uri uri = new Uri(url);

                string nonce     = this.GenerateNonce();
                string timeStamp = this.GenerateTimeStamp();

                //Generate Signature
                string sig = this.GenerateSignature(uri,
                                                    this.ConsumerKey,
                                                    this.ConsumerSecret,
                                                    this.Token,
                                                    this.TokenSecret,
                                                    this.CallBackUrl,
                                                    this.OAuthVerifier,
                                                    method.ToString(),
                                                    timeStamp,
                                                    nonce,
                                                    out outUrl,
                                                    out querystring);

                querystring += "&oauth_signature=" + this.UrlEncode(sig);

                //Convert the querystring to postData
                if (method == Web.Method.POST || method == Web.Method.DELETE)
                {
                    postData    = querystring;
                    querystring = "";
                }

                if (querystring.Length > 0)
                {
                    outUrl += "?";
                }

                ret = WebRequest(method, outUrl + querystring, postData);

                return(ret);
            }