繁体   English   中英

通过C#使用HttpWebRequest两次或更多次

[英]using HttpWebRequest two or more times via c#

我有一个问题。 我有两种使用HttpWebRequest的方法。 其中一个将消息发布到我的Facebook墙上。 另取点赞次数。
Thare是代码:

 if (Request["code"] == null)
            {
                Response.Redirect(string.Format(
                    "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
                    app_id, Request.Url.AbsoluteUri, scope));
            }
            else
            {
                Dictionary<string, string> tokens = new Dictionary<string, string>();

                string url =
                    string.Format(
                        "https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}",
                        app_id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_secret);

                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream());

                    string vals = reader.ReadToEnd();

                    foreach (string token in vals.Split('&'))
                    {
                        //meh.aspx?token1=steve&token2=jake&...
                        tokens.Add(token.Substring(0, token.IndexOf("=")),
                            token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
                    }
                }
                //get wall data
                string access_token = tokens["access_token"];
                var client = new FacebookClient(access_token);


                //post message to my wall or image
                dynamic messagePost = new ExpandoObject();


                messagePost.message = "I need to get an id of this post";

                try
                {
                    var postId = client.Post("me/feed", messagePost);
                    id_mypost = postId["id"];
                }
                catch (FacebookOAuthException ex)
                {
                    //handle oauth exception
                }
                catch (FacebookApiException ex)
                {
                    //handle facebook exception
                }
            }

此方法将消息发布到我的墙上。 Thare是第二种方法:

if (Response.BufferOutput == true)
            {
                if (Request["code"] == null)
                {

                    Response.Redirect(string.Format(
                        "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
                        app_id, Request.Url.AbsoluteUri, scope));
                }
                else
                {
                    Dictionary<string, string> tokens = new Dictionary<string, string>();

                    string url =
                        string.Format(
                            "https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}",
                            app_id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_secret);

                    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

                    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                    {
                        StreamReader reader = new StreamReader(response.GetResponseStream());

                        string vals = reader.ReadToEnd();

                        foreach (string token in vals.Split('&'))
                        {
                            //meh.aspx?token1=steve&token2=jake&...
                            tokens.Add(token.Substring(0, token.IndexOf("=")),
                                token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
                        }
                    }

                    string access_token = tokens["access_token"];
                    var client = new FacebookClient(access_token);
                    try
                    {
                    string str = client.Get("/me/feed").ToString();
                    JObject obj = JObject.Parse(str);
                    JToken jUser = obj["data"];
                    int numb = jUser.Count();
                    int id_post = 0;
                    for (int i = 0; i < numb; i++)
                    {
                        if (obj["data"][i]["id"].ToString() == id_mypost)
                        {
                            id_post = i;
                        }
                    }
                    string strr = obj["data"][id_post]["likes"].ToString();

                    string pattern = @"id";
                    int count_like = 0;
                    Regex newReg = new Regex(pattern);
                    MatchCollection matches = newReg.Matches(strr);
                    foreach (Match mat in matches)
                    {
                        count_like++;
                    }

                }
                catch (Exception)
                {

                }
            }

所以去皮是问题所在。 我使用两次HttpWebRequest。 因此,当我开帐单我的应用程序时,我遇到下一个错误:发送HTTP标头后无法重定向。

有人可以帮我吗?

发送HTTP标头后无法重定向。

这是异步操作的错误。 例如,如果您使用任何线程操作,并且在该线程中尝试Response.redirect,它将给出此错误。

语句执行之前,响应已发送给客户端。

您能告诉我错误发生在哪里吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM