【发布时间】:2013-03-27 13:59:40
【问题描述】:
我以前的应用程序无法在 WP8 上运行时遇到问题,该应用程序在 WP7 上运行良好。
这是我用于 http 请求的代码:
public void SendMessage()
{
request = WebRequest.Create(uri) as HttpWebRequest;
request.Method = "POST";
request.AllowReadStreamBuffering = true;
request.ContentType = "application/octet-stream";
try
{
// get device info
String deviceInfo = String.Format("platform,{0};os,{1};width,{2};height,{3};dpi,{4};",
Config.PLATFORM_NAME,
Environment.OSVersion.Version.ToString(),
System.Windows.Application.Current.Host.Content.ActualWidth.ToString(),
System.Windows.Application.Current.Host.Content.ActualHeight.ToString(),
96);
request.Headers["X_MX_DEVICE_INFO"] = deviceInfo;
}
catch (Exception) {}
request.BeginGetRequestStream(new AsyncCallback(ProcessRequestStream), null);
}
private void ProcessRequestStream(IAsyncResult asyncResult)
{
if (!message.IsCancelled())
{
try
{
using (Stream stream = request.EndGetRequestStream(asyncResult))
{
message.GetRequest(stream);
}
request.BeginGetResponse(new AsyncCallback(ProcessResponseStream), null);
}
catch (Exception e)
{
syncContext.Post(OnEnd, e);
}
}
else
{
syncContext.Post(OnEnd, null);
}
}
private void ProcessResponseStream(IAsyncResult asyncResult)
{
if (!message.IsCancelled())
{
try
{
response = (HttpWebResponse)request.EndGetResponse(asyncResult);
if (HttpStatusCode.OK != response.StatusCode)
{
throw new Exception("http status error: " + response.ToString());
}
syncContext.Post(SetResponse, response);
}
catch (Exception e)
{
syncContext.Post(OnEnd, e);
}
}
else
{
syncContext.Post(OnEnd, null);
}
}
private void SetResponse(object state)
{
Exception ex = null;
try
{
using (Stream stream = ((HttpWebResponse)state).GetResponseStream())
{
message.SetRespone(stream);
}
}
catch (Exception e)
{
ex = e;
}
syncContext.Post(OnEnd, ex);
}
private void OnEnd(object state)
{
message.OnEnd((Exception)state);
}
}
看起来 BeginGetResponse 的回调没有被触发。我试过 Fiddler 来看看什么响应回来了,看起来好像什么都没有回来,但只是为了 WP8。
有什么可能的原因吗?
【问题讨论】:
-
尝试 var result=request.GetResponse() 以确保,如果您使用模拟器,也可以从 fiddler 跟踪它。
-
不幸的是,我对提琴手的了解很少,因为这是我第一次需要它。使用 WP8 模拟器时,提琴手没有返回日志,而在 WP7 模拟器上,我可以按预期看到提琴手的响应。 var request = .. 似乎没有解决问题..
-
好的。如果您对服务器端进行编码,您是否跟踪您的查询真的到达了服务器端。我看到您将流发布为 application/octet-stream ,不确定但您不使用 JSON?或这样的 REST 服务。如果你有访问权限,你能调试服务器端吗?
-
我个人不编写服务器端代码,但我可以看看是否有可能从编写代码的人那里找到。这是一个相当古老的应用程序,使用我们的旧二进制系统,更新以使用我们的 json 系统会占用大量资源,所以坚持使用旧二进制文件
-
@对于服务器端的同事,您可以附加w3wp服务并在服务调用中放置断点。当服务调用时,它必须命中断点,通过这种方式您可以检查参数。如果它命中并且问题仍然存在,我强烈建议检查跟踪日志。希望帮助并解决。请给我们结果,这样我们可以了解 WP8 是否存在真正的问题。
标签: c# silverlight windows-phone-7 windows-phone-8 webrequest