C# 在发布版本中出现403错误,在调试版本中工作正常

C# 在发布版本中出现403错误,在调试版本中工作正常,c#,asp.net,visual-studio-2012,C#,Asp.net,Visual Studio 2012,我在连接到appannie.com API并获得结果的项目中工作,它在调试中工作正常,但当我发布它并尝试测试它时,我得到以下页面: 下面是C中用于此页面的代码: protected void Button1_Click(object sender, EventArgs e) { string url = "https://api.appannie.com/v1/accounts?page_index=0"; string id=

我在连接到appannie.com API并获得结果的项目中工作,它在调试中工作正常,但当我发布它并尝试测试它时,我得到以下页面:

下面是C中用于此页面的代码:

protected void Button1_Click(object sender, EventArgs e)
        {

            string url = "https://api.appannie.com/v1/accounts?page_index=0";
            string id="",temp="";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.UseDefaultCredentials = true;
            request.Proxy = WebProxy.GetDefaultProxy();
            request.Credentials = new NetworkCredential("username", "password");
            request.ContentType = "Accept: application/xml";
            request.Proxy.Credentials = CredentialCache.DefaultCredentials;
            request.Referer = "http://stackoverflow.com";
            request.Headers.Add("Authorization", "bearer **************");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream receiveStream = response.GetResponseStream();

                // Pipes the stream to a higher level stream reader with the required encoding format.
                StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
                temp = readStream.ReadToEnd();

                //TextArea1.InnerText = temp + "\n";
                string[] id_arr = temp.Split(',');
                int count = 0;
                while (count != id_arr.Length)
                {

                    if (id_arr[count].Contains("account_id"))
                    {
                        id = id_arr[count];
                        count = id_arr.Length;
                        break;
                    }

                    count++;
                }
                id = id.Substring(id.IndexOf("account_id") + 13);
                //TextArea1.InnerText += id;


                //Console.Write(readStream.ReadToEnd());
                //response.Close();
                response = null;
                //readStream.Close();
                request = null;

                string date = Calendar1.SelectedDate.ToString("yyyy-MM-dd");

                string url2 = "https://api.appannie.com/v1/accounts/" + id + "/sales?break_down=application+date" +
                                            "&start_date="+date+
                                            "&end_date="+date+
                                            "&currency=USD" +
                                            "&countries=" +
                                            "&page_index=0";


                TextArea1.InnerText = url2;


                request = (HttpWebRequest)WebRequest.Create(url2);
                request.Proxy = WebProxy.GetDefaultProxy();
                request.Proxy.Credentials = CredentialCache.DefaultCredentials;
                request.Referer = "http://stackoverflow.com";
                request.Headers.Add("Authorization", "bearer **************");
                response = (HttpWebResponse)request.GetResponse();


                receiveStream = response.GetResponseStream();

                // Pipes the stream to a higher level stream reader with the required encoding format.
                readStream = new StreamReader(receiveStream, Encoding.UTF8);
                temp = "";
                temp = readStream.ReadToEnd();
                //TextArea1.InnerText = temp;

                string[] id_arr2 = temp.Split(',');
                int count2 = 0;
                string down = "";
                string update = "";
                while (count2 != id_arr2.Length)
                {

                    if (id_arr2[count2].Contains("downloads"))
                    {
                        down = id_arr2[count2];
                        count2 = id_arr2.Length;
                        break;
                    }

                    count2++;
                }

                count2 = 0;

                while (count2 != id_arr2.Length)
                {

                    if (id_arr2[count2].Contains("update"))
                    {
                        update = id_arr2[count2];
                        count2 = id_arr2.Length;
                        break;
                    }

                    count2++;
                }


                down = down.Substring(down.IndexOf("downloads") + 12);
                update = update.Substring(update.IndexOf("update") + 9);

                //TextArea1.InnerText = "downloads : "+down+ "----- update :" + update;

                TextBox1.Text = down;
                TextBox2.Text = update;


            }



            }

发布后,以下其中一项将不生效:

资格证书 代理设置。 因此,远程api返回403。有两种方法可以进一步解决此问题:

对工作请求/响应运行fiddler跟踪,并将其与非工作请求/响应进行比较。通常,一个好的API在响应体中会有更多关于403错误原因的详细信息。。令牌无效、凭据无效等

您还可以在代码中捕获WebException,并尝试获取异常正文(如果有的话)。同样的,在Fiddler中也可以看到

>

试一试 { HttpWebResponse=HttpWebResponserequest.GetResponse; } 捕获WebException ex { //断点并查看这是什么。 string errorDetails=new StreamReaderex.Response.GetResponseStream.ReadToEnd;
}

请添加更多详细信息-代码、您已经完成的调试等。否则,任何人都无法提供帮助。抱歉,我只添加代码结果:[code]拒绝访问策略\u拒绝您的系统策略已拒绝访问请求的URL。如需帮助,请联系您的网络支持团队。[代码]策略错误指向代理/安全软件问题。尝试注释掉设置请求代理的行,然后重试。