我正在为 WP7 开发的程序有一个非常特殊的问题。首先是背景,我正在开发一个异步使用 HttpWebRequest/Response 的流式报价应用程序。我还在 Fiddler 中监控整个事情,以确保一切正常。
引号是通过始终打开的与服务器的 Http 连接进入的。登录工作正常,以及提交报价订阅请求。问题是我永远不会得到回复(它永远不会到达 EndGetResponse),除非我做以下两件事之一:
1- 通过 Fiddler 重新发出开放订阅请求 2- 通过 Fiddler 的 RequestBuilder 提交相同的请求
我试图在我的笔记本电脑上的模拟器上运行这个应用程序,但该应用程序没有工作,我得到了一个协议异常,但这是另一个线程的问题。
有任何想法吗?我认为这与通过 Fiddler 流式传输数据有关。我尝试卸载 Fiddler,禁用捕获,撤消 WinInet 中的代理设置,但没有任何效果。这让我发疯,所以你的帮助将不胜感激。
更新:我能够使用 Twitter 的流 API 重新创建它。下面是新代码。只需使用您自己的更改凭据占位符:
MainPage.xaml:
<phoneNavigation:PhoneApplicationPage
x:Class="TestHttpStreaming.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phoneNavigation="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Navigation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}">
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitleGrid is the name of the application and page title-->
<Grid x:Name="TitleGrid" Grid.Row="0">
<TextBlock Text="MY APPLICATION" x:Name="textBlockPageTitle" Style="{StaticResource PhoneTextPageTitle1Style}"/>
<TextBlock Text="page title" x:Name="textBlockListTitle" Style="{StaticResource PhoneTextPageTitle2Style}"/>
</Grid>
<!--ContentGrid is empty. Place new content here-->
<Grid x:Name="ContentGrid" Grid.Row="1">
<Button Content="Test" Height="70" HorizontalAlignment="Left" Margin="163,149,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
</Grid>
</Grid>
</phoneNavigation:PhoneApplicationPage>
MainPage.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
namespace TestHttpStreaming
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
}
Uri uri = null;
HttpWebRequest request = null;
byte[] buffer = new byte[1024];
Stream stream = null;
private void button1_Click(object sender, RoutedEventArgs e)
{
uri = new Uri("http://stream.twitter.com/1/statuses/sample.json?delimited=length", UriKind.Absolute);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
request.Credentials = new NetworkCredential("[username]", "[password]");
request.BeginGetResponse(new AsyncCallback(this.EndGetResponseStream), null);
}
void EndGetResponseStream(IAsyncResult result)
{
try
{
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
stream = response.GetResponseStream();
IAsyncResult iarRead = stream.BeginRead(buffer, 0, 1024, new AsyncCallback(StreamingReadCallBack), null);
}
finally
{
}
}
private void StreamingReadCallBack(IAsyncResult asyncResult)
{
int read = stream.EndRead(asyncResult);
}
}
}