在发布版本中获得403错误,并在调试版本中正常工作

时间:2014-03-30 06:22:37

标签: c# asp.net visual-studio-2012

我在项目中工作,连接到appannie.com API并获得结果,它在调试中运行良好但是当我发布它并尝试测试它时,我得到了这个页面: enter image description here

以下是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;


            }



            }

1 个答案:

答案 0 :(得分:0)

发布后,以下其中一项无法生效:

  1. 证书
  2. 代理设置。
  3. 因此,远程api正在回馈403. 2种方法可以进一步扭转这种情况:

    1. 对工作请求/响应运行fiddler跟踪,并将其与非工作请求/响应进行比较。通常,一个好的API在响应正文中有更多细节,说明为什么它是403错误..(令牌无效,凭证无效等)。

    2. 您还可以在代码中捕获WebException,并尝试获取异常体(如果有)。在Fiddler中也可以看到相同的内容。

    3. <<代码格式化拒绝在我的浏览器上工作。请使用下面未格式化的代码>>

      试 {  HttpWebResponse response =(HttpWebResponse)request.GetResponse(); } catch(WebException ex) {  //断点,看看这是什么。  string errorDetails = new StreamReader(ex.Response.GetResponseStream())。ReadToEnd(); }