ios - 在设备上使用 Monotouch 上传 xml 失败

标签 ios xamarin.ios webclient

我正在尝试通过 Web 客户端在 Monotouch 上发送 XML。虽然一切都在 iOS 模拟器上运行,但当应用程序在设备上运行时,我遇到了一个异常。这是代码。

try
{
   Byte[] sendData = System.Text.Encoding.UTF8.GetBytes(someXML) ;
   WebClient client = new WebClient();
   Byte[] forResp;
   client.Headers.Add("Content-Type","text/xml");

   bresp = wc.UploadData(URL, sendData);
   string resp = System.Text.Encoding.ASCII.GetString(forResp);
   XmlDocument xresp = new XmlDocument();
   xresp.LoadXml(resp);
   return xresp;
}
catch
{
    // error
}

我尝试了 WebClient,因为我遇到了与 HttpWebRequest 相同的问题。

异常(exception):

The request timed out
System.Net.WebException: The request timed out
  at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00065] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:830 
  at System.Net.HttpWebRequest.GetResponse () [0x0000e] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:836 
  at System.Net.WebClient.GetWebResponse (System.Net.WebRequest request) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebClient.cs:1433 
  at System.Net.WebClient.ReadAll (System.Net.WebRequest request, System.Object userToken) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebClient.cs:866 
  at System.Net.WebClient.UploadDataCore (System.Uri address, System.String method, System.Byte[] data, System.Object userToken) [0x00038] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebClient.cs:462 
  at System.Net.WebClient.UploadData (System.Uri address, System.String method, System.Byte[] data) [0x00035] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebClient.cs:441 

最佳答案

堆栈跟踪告诉我们,超时发生在 WebClient 尝试从服务器获取响应时(而不是在将数据上传到服务器时)。这意味着对服务器的访问不是问题(以及为什么堆栈跟踪非常有用;-)

接下来的步骤取决于您是否可以控制服务器。如果您控制服务器,则检查它的日志以查找请求并查看它是如何处理的。如果可能,还要确保您在模拟器和设备之间以相同方式接收/解码数据。

如果您不控制服务器,那么就很难知道其中发生了什么。例如。也许它不喜欢请求(即服务器代码可以根据任何条件决定不回答,包括用户代理)。

在这种情况下,使用wireshark 之类的工具通常是最好的选择。比较来自 iOS 模拟器和设备的 session 。

  • 请求是否相同?例如您上传的数据可能因其他原因而有所不同;

  • 服务器是否正在响应设备?具有相同的状态代码?在同一时间范围内?

如果一切都相同,那么您可能发现了错误。请将其(连同上述信息和文件)报告给http://bugzilla.xamarin.com

否则你应该有更精确的线索来调试问题和/或更新问题更具体:-)

关于ios - 在设备上使用 Monotouch 上传 xml 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10224382/

相关文章:

ios - 无法更新本地定时通知内容

ios - iphone NSManagedObject - 解除分配的正确方法?

ios - 确定 iOS 应用程序是否通过 Siri 启动

xamarin.ios - MonoTouch.对话框 : Advanced Editing

c# - 下载网站数据的最快方法 C#

c# - 如何避免Webclient下载0字节文件错误

ios - ionic 2 - 在 ipad 设备上运行应用程序

c# - Xamarin Studio iOS 程序集错误

IOS:创建全屏 'overlay' UIView (Monotouch)

webclient - 为什么我无法使用 webclient 访问 Yobit API