API WebService不响应

时间:2018-07-24 19:47:39

标签: c# api web-services

这基本上是我第一次使用C#处理API,因此我阅读并尝试创建以便可以处理JSON,但是我没有得到任何响应,试图以标签文本显示它,但是我没有得到任何错误或任何响应。

应该在带有基本身份验证的答案的标签中显示JSON,因此,我就可以处理它,因为如果我通过POSTMAN登录,就可以看到JSON,但是如果我运行代码,则全部我看不到什么,甚至包裹在一个字符串中。

public partial class callUni : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string strResponse = string.Empty;
            strResponse = makeRequest();
            answer.Text = strResponse;
    }

    public string makeRequest()
    {
        string strRequest = string.Empty;
        try
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://unicard-api.asf.edu.mx:8443/api/Acessos/Entradas");
            request.Credentials = GetCredential();
            request.PreAuthenticate = true;
            request.Method = httpMethod.ToString();
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new ApplicationException("error code = " + response.StatusCode);
                }
                //Vamos a procesar el JSON que viene de UNICARD
                using (Stream responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                    {
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            strRequest = reader.ReadToEnd();
                        }
                    }
                }

            }
        }
        catch (Exception e) { };
        return strRequest;
    }

    private CredentialCache GetCredential()
    {
        string url = @"https://unicard-api.asf.edu.mx:8443/api/Acessos/Entradas";
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
        CredentialCache credentialCache = new CredentialCache();
        credentialCache.Add(new System.Uri(url), "Basic", new NetworkCredential(ConfigurationManager.AppSettings["xxxxx"], ConfigurationManager.AppSettings["xxxx"]));
        return credentialCache;
    }
}

}

1 个答案:

答案 0 :(得分:0)

您说“我没有收到任何错误,也没有任何响应。”,但我认为您收到了错误,但此处的行对您隐藏了:

catch (Exception e) { };

尝试在catch块中记录或显示e.ToString(),然后从那里进行调查。

作为附带说明,Microsoft明确表示不要抛出ApplicationException。查找更相关的Exception类以使用或抛出Exception。 https://msdn.microsoft.com/en-us/library/system.applicationexception%28v=vs.110%29.aspx#Remarks