I am having a very peculiar problem with a program that I am developing for WP7. First a background, I am developing a streaming quote application that uses HttpWebRequest/Response asynchronously. I am also monitoring the whole thing in Fiddler to make sure everything is right.
The quotes are coming in through an always open Http connection to the server. Logging in works fine, as well as submitting the quote subscription request. The problem is that I never get a response back (it never gets to EndGetResponse) unless I do one of two things:
1- Reissue the open subscription request through Fiddler 2- Submit an identical request through Fiddler's RequestBuilder
I tried to run this app on the simulator on my laptop but the app didn't work, I got a protocol exception but that's a question for a different thread.
Any ideas? I think this has to do with streaming data through Fiddler. I tried uninstalling Fiddler, disable capturing, undo the proxy setting in WinInet but nothing has worked. This is driving me crazy, so your help would be greatly appreciated.
Update: I was able to recreate this with Twitter's streaming API. Below is the new code. Just change the credentials placeholder with your own:
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}"/>
</Gr开发者_运维百科id>
<!--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);
}
}
}
Not sure if anybody is still facing this issue. I had this issue and got fixed once I set:
request.AllowReadStreamBuffering = false;
This is with the latest framework.
Please check to see :
Have you enabled the streaming mode on the tool strip menu bar?
2. Have you turned off the "Enable automatic responses" ?
Hope this help.
This seems to be a bug in the framework/tools/emulator: http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series/thread/ed07aaba-5bbe-4cc7-b008-67fa87a83ace
精彩评论