WebRequest.GetResponseStream() returns null.
Last post 05-11-2008 2:29 PM by sladapter. 3 replies.
Sort Posts:
05-11-2008 10:34 AM
WebRequest.GetResponseStream() returns null.

Hello,

 I working on Silverlight 2 Beta1 application that reads HTML content from other URL page. I'm experiencing Exception:

System.ArgumentNullException: Value cannot be null.
Parameter name: stream

Here is the code:

private void UserControl_Loaded(object sender, RoutedEventArgs e)

{

string urlUPVuri = @"http://localhost/test.php";

saveURL(urlUPVuri);

}

private void saveURL(string url)

{

try

{

WebRequest request = (WebRequest)WebRequest.Create(new Uri(url));request.BeginGetResponse(new AsyncCallback(ReadCallback), request);

}

catch (Exception ex)

{

txtTest.Text = ex.ToString();

}

}

private void ReadCallback(IAsyncResult asynchronousResult)

{

try

{

WebRequest request = (WebRequest)asynchronousResult.AsyncState;

WebResponse response = (WebResponse)request.EndGetResponse(asynchronousResult);

Stream respStream = response.GetResponseStream();

StreamReader streamReader1 = new StreamReader(respStream);

string resultString = streamReader1.ReadToEnd();

txtTest.Text = resultString;

}

catch (Exception ex)

{

txtTest.Text = ex.ToString();

}

}

However eqvivalent ASP.NET code works well. So it might be access policy problem, but i do have clientaccesspolicy.xml file on server and i can request it from my browser over url: http://localhost/clientaccesspolicy.xml

Here is ASP.NET code:

private string saveURL(string url)

{

string content = "";if (!string.IsNullOrEmpty(url))

{

WebRequest webRequest = WebRequest.Create(url);

WebResponse webResponse = webRequest.GetResponse();StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));

content = sr.ReadToEnd();

}

return content;

}

Any idea what could be wrong ?

AlbertasA

Joined on 05-11-2008
Posts 3
05-11-2008 12:59 PM
Marked as Answer
Re: WebRequest.GetResponseStream() returns null.

Try this. for this purpose use WebClient is easier:

private void saveURL(string url)

{

  WebClient wc = new WebClient();         
  wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
  wc.DownloadStringAsync(new Uri(url));

}

void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                HtmlPage.Window.Alert(e.Result); //e.Rresult should be the returning string of your page.
            }
        }

 

If you had cross-domain issue, usually e.Error would say "Download failure." If you did not get an error, but returning string is empty that might be something else.

Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

sladapter

Joined on 03-05-2008
Indiana, US
Posts 404
05-11-2008 1:44 PM
Re: WebRequest.GetResponseStream() returns null.

Thank you,

Your solution works for my situation. Althought the origial idea is still in question.

AlbertasA

Joined on 05-11-2008
Posts 3
05-11-2008 2:29 PM
Re: WebRequest.GetResponseStream() returns null.

 I tried your original code and it works too. I got the same result as using the code I posted. Both should work, just less coding using WebClient.

 You can check asynchronousResult.IsCompleted before you try to get the stream.


Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

sladapter

Joined on 03-05-2008
Indiana, US
Posts 404