通过c#使用HttpWebRequest两次或两次以上

本文关键字:两次 HttpWebRequest 使用 通过 | 更新日期: 2023-09-27 18:29:05

我有一个问题。我有两个使用HttpWebRequest的方法。其中一个在我的脸书墙上发布了一条消息。另一个统计点赞数
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
                }
            }

这种方法将消息张贴到我的墙上。解冻是第二种方法:

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标头后无法重定向。

有人能帮我吗?

通过c#使用HttpWebRequest两次或两次以上

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

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

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

你能告诉我错误到底发生在哪里吗?