web-services - 为什么 Windows Phone 应用程序的 REST 服务中会出现 NotFound 错误?

标签 web-services rest windows-phone-8 windows-phone http-status-code-404

我尝试从 Windows Phone 8 应用程序连接 REST Web 服务。

它工作了好几个星期,但在没有任何改变之后我得到了这个一般错误:

System.Net.WebException: The remote server returned an error: NotFound.

我尝试通过在线 REST 客户端进行测试,服务工作正常

我尝试通过以下代码处理异常并将其解析为 webException :

    var we = ex.InnerException as WebException;
    if (we != null)
    {
        var resp = we.Response as HttpWebResponse;
        response.StatusCode = resp.StatusCode;

我没有得到更多信息,最终响应代码是:“NotFound”

任何人都知道可能导致此错误的原因吗?

服务器上已实现了受信任的证书。拥有服务器的人建议为服务器设置一个 DNS 条目,该条目应该位于客户 DNS 或电话主机文件中。我所做的并工作了一段时间,但现在它不起作用,但我检查了这一点没有什么改变

这是 Get Request 的示例,它可以在 Windwos Store 应用程序上正常工作:

async Task<object> GetHttps(string uri, string parRequest, Type returnType, params string[] parameters)
        {
            try
            {
                string strRequest = ConstructRequest(parRequest, parameters);
                string encodedRequest = HttpUtility.UrlEncode(strRequest);

                string requestURL = BackEndURL + uri + encodedRequest;

                HttpWebRequest request = HttpWebRequest.Create(new Uri(requestURL, UriKind.Absolute)) as HttpWebRequest;

                request.Headers["applicationName"] = AppName;
                request.Headers["applicationPassword"] = AppPassword;
                if (AppVersion > 1)
                    request.Headers["applicationVersion"] = AppVersion.ToString();
                request.Method = "GET";

                request.CookieContainer = cookieContainer;

                var factory = new TaskFactory();
                var getResponseTask = factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
                HttpWebResponse response = await getResponseTask as HttpWebResponse;
              //  string s = response.GetResponseStream().ToString();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    XmlSerializer serializer = new XmlSerializer(returnType);
                    object obj = serializer.Deserialize(response.GetResponseStream());
                    return obj;
                }
                else
                {
                    var Instance = Activator.CreateInstance(returnType);
                    (Instance as ResponseBase).NetworkError = true;
                    (Instance as ResponseBase).StatusCode = response.StatusCode;
                    return Instance;
                }

            }
            catch (Exception ex)
            {
                return HandleException(ex, returnType);
            }
        }

我尝试监视来自模拟器的连接,但在连接中发现了此错误:

**

Authentication failed because the remote party has closed the transport stream.

**

最佳答案

您看到客户端在服务中实现了服务器端证书。您的手机上安装了该证书吗?这可能是导致 NotFound 错误的原因。请问,在测试应用程序之前,您能否尝试在手机或模拟器 Internet Explorer 中导航到该服务?如果这样做,您可以看到该服务在模拟器/手机互联网浏览器中运行吗?也许那时 Internet Explorer 会询问您有关安装证书的信息,然后您可以打开您的应用程序,它就可以工作了。

还要记住,如果您在模拟器中进行测试,每次关闭模拟器时,状态都会丢失,因此您需要再次重复安装证书的操作。

希望这有帮助。

关于web-services - 为什么 Windows Phone 应用程序的 REST 服务中会出现 NotFound 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20470728/

相关文章:

c# - 如何将 ListView DataTemplate 中的按钮绑定(bind)到 ViewModel 中的命令? (MVVMLight)

c# - 导航期间多次调用 ValueConverter

json - 如何在 Lucerne 中捕获解析错误(常见的 lisp)

c# - for 循环中的每次迭代都会覆盖整个数组的值

iphone - App Store中iPhone应用程序的不同数据源

wcf - 使用消息安全性时的 WS-Security 开销

c# - HttpGet 中查询字符串的替代方案用于发送数据?

java - 替代响应 spring boot api 休息

java - 如何将 REST Web 服务部署到 AWS?

java - 日期和日期时间应该如何序列化 SOAP (xml) 消息