Ejemplo n.º 1
0
        public async Task CreateHttpRequest_ShouldCreateHttpRequest()
        {
            var accessToken = "accessToken";

            _accessTokensServiceMock
            .Setup(x => x.GenerateAccessToken(_connectionString, _url, null, null))
            .Returns(accessToken)
            .Verifiable();

            var httpRequest = _httpRequestFactory.CreateHttpRequest(
                _connectionString,
                _hubMethodName,
                _event,
                _url
                );

            Assert.That(httpRequest, Is.Not.Null);
            Assert.That(httpRequest, Has.Property(nameof(HttpRequestMessage.Method)).EqualTo(HttpMethod.Post));
            Assert.That(
                httpRequest,
                Has.Property(nameof(HttpRequestMessage.RequestUri)).Property(nameof(Uri.AbsoluteUri)).EqualTo(_url)
                );

            AssertThatHasHeader(httpRequest, "Authorization", "Bearer " + accessToken);
            AssertThatHasHeader(httpRequest, "Accept", "application/json");

            var content = await httpRequest.Content.ReadAsStringAsync();

            var jObject = JObject.Parse(content);

            Assert.That(jObject["Target"].Value <string>(), Is.EqualTo(_hubMethodName));
            Assert.That(jObject["Arguments"], Is.Not.Null);
        }
        private async Task <Http.IHttpRequest> CreateHttpRequestAsync(Uri uri, string httpMethod)
        {
            var request = HttpRequestFactory.CreateHttpRequest(uri);

            request.Method = httpMethod;
            await AddCommonRequestHeaders(request);

            return(request);
        }
Ejemplo n.º 3
0
        protected void upload_Click(object sender, EventArgs e)
        {
            var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.POST) as HttpPost;

            httpRequest.Token       = Session["oauth_token"].ToString();
            httpRequest.TokenSecret = Session["oauth_token_secret"].ToString();
            string url = "http://api.t.sina.com.cn/statuses/upload.xml?";

            this.resultTextBox2.Text = httpRequest.RequestWithPicture(url, "status=" + HttpUtility.UrlPathEncode(statusText.Text), filepath.Text);
        }
Ejemplo n.º 4
0
        protected void update_Click(object sender, EventArgs e)
        {
            var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.POST);

            httpRequest.Token       = Session["oauth_token"].ToString();
            httpRequest.TokenSecret = Session["oauth_token_secret"].ToString();
            var url = "http://api.t.sina.com.cn/statuses/update.xml?";

            resultTextBox2.Text = httpRequest.Request(url, "status=" + HttpUtility.UrlEncode(statusText.Text));
        }
Ejemplo n.º 5
0
        protected void sinaButton_Click(object sender, EventArgs e)
        {
            HttpGet httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET) as HttpGet;

            httpRequest.GetRequestToken();
            string url = httpRequest.GetAuthorizationUrl();

            Session["oauth_token"]        = httpRequest.Token;
            Session["oauth_token_secret"] = httpRequest.TokenSecret;
            Response.Redirect(url + "&oauth_callback=http://localhost:3668/Default.aspx");
        }
Ejemplo n.º 6
0
        protected void invoke_Click(object sender, EventArgs e)
        {
            BaseHttpRequest httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET);

            httpRequest.Token        = Session["oauth_token"].ToString();
            httpRequest.TokenSecret  = Session["oauth_token_secret"].ToString();
            httpRequest.userid       = Session["oauth_user_id"].ToString();
            httpRequest.UserRemoteIP = Request.UserHostAddress;
            string basic = "http://api.t.sina.com.cn/";
            string url   = "";

            switch (this.DropDownList.SelectedIndex)
            {
            case 0:
                url = basic + "statuses/public_timeline.xml";
                break;

            case 1:
                url = basic + "statuses/friends_timeline.xml";
                break;

            case 2:
                url = basic + "statuses/user_timeline.xml";
                break;

            case 3:
                url = basic + "statuses/mentions.xml";
                break;

            case 4:
                url = basic + "statuses/comments_timeline.xml";
                break;

            case 5:
                url = basic + "statuses/comments_by_me.xml";
                break;

            case 6:
                url = basic + "statuses/followers.xml";
                break;

            case 7:
                url = basic + "users/show.json?user_id=" + httpRequest.userid;
                break;

            default:
                break;
            }
            this.resultText.Text = httpRequest.Request(url, string.Empty);
        }
Ejemplo n.º 7
0
        public string UploadHttpWebRequestByoAuth(string userID, string pwd, string status, string filename, byte[] imageStream, string apiToken, string apiTokenSecret)
        {
            //SinaApiService myApi = new SinaApiService();
            //string myResult = myApi.status_uploadbyoauth(userID, pwd, "xml", status,
            //    WebConfigurationManager.AppSettings["consumerKey"],
            //    WebConfigurationManager.AppSettings["consumerSecret"], apiToken,
            //    apiTokenSecret, filename, imageStream);


            var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.POST) as HttpPost;

            httpRequest.Token        = apiToken;
            httpRequest.TokenSecret  = apiTokenSecret;
            httpRequest.UserRemoteIP = "127.0.0.1";
            string url      = "http://api.t.sina.com.cn/statuses/upload.xml?";
            string myResult = httpRequest.RequestWithPicture(url, "status=" + HttpUtility.UrlEncode(status), filename, imageStream);

            return(myResult);
        }
Ejemplo n.º 8
0
    protected void Button6_Click(object sender, EventArgs e)
    {
        //oAuthSina oauth = new oAuthSina();
        //oauth.appKey = "1421367737";
        //oauth.appSecret = "2be4da41eb329b6327b7b2ac56ffbe6e";
        //oauth.RequestTokenGet();
        //string url = oauth.AuthorizationGet();
        //new Common.Cookie().setCookie("oauth_token", oauth.token, 100000);
        //new Common.Cookie().setCookie("oauth_token_secret", oauth.tokenSecret, 100000);
        //Response.Redirect(url + "&oauth_callback=" + baseurl + "/top/microblog/record.aspx?typ=sina");

        HttpGet httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET) as HttpGet;

        httpRequest.AppKey    = "1421367737";
        httpRequest.AppSecret = "2be4da41eb329b6327b7b2ac56ffbe6e";
        httpRequest.GetRequestToken();
        string url = httpRequest.GetAuthorizationUrl();

        Session["oauth_token"]        = httpRequest.Token;
        Session["oauth_token_secret"] = httpRequest.TokenSecret;
        Response.Redirect(url + "&oauth_callback=" + baseurl + "/top/groupbuy/record.aspx?typ=sina");
    }
Ejemplo n.º 9
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET) as HttpGet;

                if (Request["oauth_verifier"] != null)
                {
                    httpRequest.Token       = Session["oauth_token"].ToString();
                    httpRequest.TokenSecret = Session["oauth_token_secret"].ToString();
                    httpRequest.Verifier    = Request["oauth_verifier"];
                    httpRequest.GetAccessToken();
                    Session["oauth_token"]        = httpRequest.Token;
                    Session["oauth_token_secret"] = httpRequest.TokenSecret;
                    Response.Redirect("/Sample.aspx");
                }
                if (Session["oauth_token"] != null)
                {
                    this.sinaButton.Text = "连接成功";
                }
            }
        }
Ejemplo n.º 10
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                HttpGet httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET) as HttpGet;
                if (Request["oauth_verifier"] != null)
                {
                    httpRequest.Token       = Session["oauth_token"].ToString();
                    httpRequest.TokenSecret = Session["oauth_token_secret"].ToString();
                    httpRequest.Verifier    = Request["oauth_verifier"];
                    httpRequest.GetAccessToken();
                    Session["oauth_token"]        = httpRequest.Token;
                    Session["oauth_token_secret"] = httpRequest.TokenSecret;
                    Session["oauth_user_id"]      = httpRequest.userid;

                    Response.Redirect("/Default.aspx");
                }
                if (Session["oauth_token"] != null)
                {
                    this.sinaButton.Text = "Á¬½Ó³É¹¦";
                    //this.Label4.Text = httpRequest.Request("http://api.t.sina.com.cn/users/show.xml?", "screen_name=0351netcn");
                }
            }
        }
Ejemplo n.º 11
0
    protected void Page_Load(object sender, EventArgs e)
    {
        //参数初始化
        baseurl = "http://www.7fshop.com";

        Common.Cookie cookie1    = new Common.Cookie();
        string        taobaoNick = cookie1.getCookie("nick");

        //过期判断
        if (string.IsNullOrEmpty(taobaoNick))
        {
            Response.Write("<script>parent.location.href='http://container.open.taobao.com/container?appkey=12159997'</script>");
            Response.End();
            return;
        }

        Rijndael_ encode = new Rijndael_("tetesoft");
        string    nick   = encode.Decrypt(taobaoNick);

        string typ    = utils.NewRequest("typ", utils.RequestType.QueryString);
        string verify = string.Empty;

        if (typ == "qq")
        {
            appKey    = "d3225497956249cbb13a7cb7375d62bd";
            appSecret = "6cf7a3274cb676328e77dff3e203061d";

            tokenKey    = utils.NewRequest("oauth_token", utils.RequestType.QueryString);
            tokenSecret = Session["tokenSecret"].ToString();
            verify      = utils.NewRequest("oauth_verifier", utils.RequestType.QueryString);
            //获取账户信息
            GetAccessToken(appKey, appSecret, tokenKey, tokenSecret, verify);

            //发送微博
            List <Parameter> parameters = new List <Parameter>();

            //身份验证
            OauthKey oauthKey = new OauthKey();
            oauthKey.customKey     = appKey;
            oauthKey.customSecrect = appSecret;
            oauthKey.tokenKey      = tokenKey;
            oauthKey.tokenSecrect  = tokenSecret;

            //图片信息
            List <Parameter> files = new List <Parameter>();

            QWeiboRequest request = new QWeiboRequest();
            int           nKey    = 0;
            if (request.AsyncRequest("http://open.t.qq.com/api/user/info", "POST", oauthKey, parameters, files, new AsyncRequestCallback(RequestCallback), out nKey))
            {
                //textOutput.Text = "请求中...";
            }

            string sql   = "SELECT COUNT(*) FROM TopMicroBlogAccount WHERE nick = '" + nick + "' AND uid='" + weiboName + "' AND typ = 'qq'";
            string count = utils.ExecuteString(sql);
            if (count == "0")
            {
                //写入数据库
                sql = "INSERT INTO TopMicroBlogAccount (nick, uid, typ, tokenKey, tokenSecrect) VALUES ('" + nick + "', '" + weiboName + "', 'qq', '" + tokenKey + "', '" + tokenSecret + "')";
                utils.ExecuteNonQuery(sql);
            }
        }
        else if (typ == "sina")
        {
            tokenKey    = Session["oauth_token"].ToString();
            tokenSecret = Session["oauth_token_secret"].ToString();
            verify      = utils.NewRequest("oauth_verifier", utils.RequestType.QueryString);

            HttpGet httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET) as HttpGet;
            httpRequest.AppKey      = "1421367737";
            httpRequest.AppSecret   = "2be4da41eb329b6327b7b2ac56ffbe6e";
            httpRequest.Token       = tokenKey;
            httpRequest.TokenSecret = tokenSecret;
            httpRequest.Verifier    = verify;
            httpRequest.GetAccessToken();
            tokenKey    = httpRequest.Token;
            tokenSecret = httpRequest.TokenSecret;
            weiboName   = httpRequest.UserId;

            string sql   = "SELECT COUNT(*) FROM TopMicroBlogAccount WHERE nick = '" + nick + "' AND uid='" + weiboName + "' AND typ = 'sina'";
            string count = utils.ExecuteString(sql);
            if (count == "0")
            {
                sql = "INSERT INTO TopMicroBlogAccount (nick, uid, typ, tokenKey, tokenSecrect) VALUES ('" + nick + "', '" + weiboName + "', 'sina', '" + tokenKey + "', '" + tokenSecret + "')";
                utils.ExecuteNonQuery(sql);
            }
        }
        //Response.Write(sql);
        //跳转
        Response.Redirect("weiboindex.aspx");
    }
Ejemplo n.º 12
0
        private void SendMicroBlog(string nick, string content, string filepath, string index)
        {
            //过滤过长的内容
            if (content.Length > 140)
            {
                content = content.Substring(0, 140);
            }

            string    appKey    = "d3225497956249cbb13a7cb7375d62bd";
            string    appSecret = "6cf7a3274cb676328e77dff3e203061d";
            string    sql       = "SELECT * FROM TopMicroBlogAccount WHERE nick = '" + nick + "'";
            DBSql     db        = new DBSql();
            DataTable dt        = db.GetTable(sql);

            textBox1.AppendText("\r\n" + dt.Rows.Count.ToString());
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                try
                {
                    if (dt.Rows[i]["typ"].ToString() == "qq")
                    {
                        //MessageBox.Show(content);
                        //发送微博
                        List <Parameter> parameters = new List <Parameter>();
                        parameters.Add(new Parameter("content", content));

                        //身份验证
                        OauthKey oauthKey = new OauthKey();
                        oauthKey.customKey     = appKey;
                        oauthKey.customSecrect = appSecret;
                        oauthKey.tokenKey      = dt.Rows[i]["tokenKey"].ToString();
                        oauthKey.tokenSecrect  = dt.Rows[i]["tokenSecrect"].ToString();

                        //图片信息
                        List <Parameter> files = new List <Parameter>();
                        if (filepath != "")
                        {
                            files.Add(new Parameter("pic", DownPic(filepath)));
                        }

                        QWeiboRequest request = new QWeiboRequest();
                        int           nKey    = 0;
                        if (request.AsyncRequest("http://open.t.qq.com/api/t/add_pic", "POST", oauthKey, parameters, files, new AsyncRequestCallback(RequestCallback), out nKey))
                        {
                            //textOutput.Text = "请求中...";
                        }

                        sql = "UPDATE TopMicroBlogAuto SET num" + index + " = num" + index + " + 1 WHERE nick = '" + dt.Rows[i]["nick"].ToString() + "'";
                        db.ExecSql(sql);
                        textBox1.AppendText("\r\n" + sql);

                        sql = "INSERT INTO TopMicroBlogSendLog (result, nick, uid, typ, content, auto) VALUES ('','" + nick + "','" + dt.Rows[i]["uid"].ToString() + "','qq','" + content + "','" + index + "')";
                        db.ExecSql(sql);
                        textBox1.AppendText("\r\nqq-" + sql);
                    }
                    else if (dt.Rows[i]["typ"].ToString() == "sina")
                    {
                        string   url         = string.Empty;
                        string   result      = string.Empty;
                        HttpPost httpRequest = HttpRequestFactory.CreateHttpRequest(Method.POST) as HttpPost;
                        httpRequest.AppKey      = "1421367737";
                        httpRequest.AppSecret   = "2be4da41eb329b6327b7b2ac56ffbe6e";
                        httpRequest.Token       = dt.Rows[i]["tokenKey"].ToString();
                        httpRequest.TokenSecret = dt.Rows[i]["tokenSecrect"].ToString();
                        if (filepath != "")
                        {
                            url    = "http://api.t.sina.com.cn/statuses/upload.xml?";
                            result = httpRequest.RequestWithPicture(url, "status=" + HttpUtility.UrlPathEncode(content.Replace("=", "")), DownPic(filepath));
                            //result = httpRequest.Request(url, "status=" + HttpUtility.UrlEncode(content,Encoding.UTF8));
                        }
                        else
                        {
                            url    = "http://api.t.sina.com.cn/statuses/update.xml?";
                            result = httpRequest.Request(url, "status=" + HttpUtility.UrlEncode(content, Encoding.UTF8));
                        }

                        sql = "UPDATE TopMicroBlogAuto SET num" + index + " = num" + index + " + 1 WHERE nick = '" + dt.Rows[i]["nick"].ToString() + "'";
                        db.ExecSql(sql);
                        textBox1.AppendText("\r\n" + sql);

                        sql = "INSERT INTO TopMicroBlogSendLog (result, nick, uid, typ, content, auto) VALUES ('','" + nick + "','" + dt.Rows[i]["uid"].ToString() + "','sina','" + content + "','" + index + "')";
                        db.ExecSql(sql);

                        textBox1.AppendText("\r\nsina-" + sql);
                    }
                }
                catch (Exception e)
                {
                    textBox1.AppendText("\r\n" + e.Message);
                    textBox1.AppendText("\r\n" + e.StackTrace);
                    continue;
                }
            }
        }
Ejemplo n.º 13
0
        public string Call(string api, string parameter, string filename, byte[] bytes)
        {
            var url = api;

            if (!string.IsNullOrEmpty(parameter))
            {
                if (api.IndexOf("?") > 0)
                {
                    url += "&";
                }
                else
                {
                    url += "?";
                }

                var postdata = ParsePostData(parameter);

                url += postdata;
            }

            var authorization = GetAuthorizationHead(url, "POST");

            WebHeaderCollection header = new WebHeaderCollection();

            header.Add(HttpRequestHeader.Authorization, authorization);

            var boundary = "OAuthAPI";

            var cols   = HttpUtility.ParseQueryString(parameter);
            var status = "";

            if (cols["status"] != null)
            {
                status = HttpUtility.UrlEncode(cols["status"]);
            }
            float?latVal = null;

            if (cols["lat"] != null)
            {
                latVal = float.Parse(cols["lat"]);
            }
            float?longVal = null;

            if (cols[""] != null)
            {
                longVal = float.Parse(cols["long"]);
            }

            var body = PackImage(boundary, filename, bytes, oauthBase.AppKey, status, latVal, longVal);

            string contentType = string.Format("multipart/form-data; boundary={0}", boundary);

            header.Add(HttpRequestHeader.ContentType, contentType);

            var request = HttpRequestFactory.CreateHttpRequest(Proxy);

            request.Encode = Encoding.GetEncoding(ContentEncoding);

            var content = request.Post(api, body, header);

            return(content);
        }
Ejemplo n.º 14
0
        public string Call(HttpMethod method, string api, string parameter)
        {
            if (method == HttpMethod.GET)
            {
                if (!string.IsNullOrEmpty(parameter))
                {
                    api += "?source=" + oauthBase.AppKey + "&" + parameter;
                }
            }
            else if (method == HttpMethod.POST)
            {
                if (!string.IsNullOrEmpty(parameter))
                {
                    if (api.IndexOf("?") > 0)
                    {
                        api += "&";
                    }
                    else
                    {
                        api += "?";
                    }

                    var postdata = ParsePostData(parameter);

                    api += postdata;
                }
            }

            var    url = api;
            string outUrl;
            string querystring;

            var nonce     = oauthBase.GenerateNonce();
            var timestamp = oauthBase.GenerateTimeStamp();
            var signature = oauthBase.GenerateSignature(
                new Uri(url),
                oauthBase.AppKey,
                oauthBase.AppSecret,
                oauthBase.Token,
                oauthBase.TokenSecret,
                method.ToString(),
                timestamp,
                nonce,
                out outUrl,
                out querystring
                );

            querystring += "&oauth_signature=" + HttpUtility.UrlEncode(signature);

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

            var request = HttpRequestFactory.CreateHttpRequest(Proxy);

            var content = "";

            if (method == HttpMethod.GET)
            {
                content = request.Get(outUrl);
            }
            else if (method == HttpMethod.POST)
            {
                content = request.Post(outUrl, querystring);
            }

            return(content);
        }