【问题标题】:API WebService Not responding anythingAPI WebService 无响应
【发布时间】:2019-01-01 12:56:55
【问题描述】:

这基本上是我第一次使用 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;
    }
}

}

【问题讨论】:

    标签: c# api web-services


    【解决方案1】:

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

    catch (Exception e) { };
    

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

    作为旁注,Microsoft 明确表示不要抛出 ApplicationException。要么找到一个更相关的异常类来使用,要么抛出异常。 https://msdn.microsoft.com/en-us/library/system.applicationexception%28v=vs.110%29.aspx#Remarks

    【讨论】:

    • 完全正确...得到它连接到非基本认证!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    相关资源
    最近更新 更多