即使请求被停止,代码也会输出两次错误
本文关键字:输出 错误 两次 请求 代码 | 更新日期: 2023-09-27 18:18:35
我有一个问题。
当我在此代码中得到错误时,即使我调用的函数终止响应,它也会输出两次错误,请注意OutputError:
private dynamic GetOauthTokens(string code)
{
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}&client_secret={2}&code={3}",
myAppId, HttpUtility.UrlEncode(myLoginRedirectUrl), myAppSecret, code);
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string retVal = reader.ReadToEnd();
foreach (string token in retVal.Split('&'))
{
tokens.Add(token.Substring(0, token.IndexOf("=")),
token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
}
}
}
catch (Exception exception)
{
OutputError("Code", exception.Message);
}
return tokens;
}
,这里是我处理它的地方:
protected void OutputError(string error, string message)
{
object obj = new { Status = false, Error = error, Message = message };
string objJson = JsonConvert.SerializeObject(obj);
myHttpContext.Response.Write("LinkedLook.getJson(" + objJson + ");");
myHttpContext.Response.End();
}
由于某种原因,它吐出了两次…我做错了什么?
设置调试器,在"ouputerror"函数的开始处设置一个断点。如果它第二次击中它,检查堆栈跟踪,看看它来自哪里。如果它两次来自UI,那么对firebug做同样的事情。