Skip to main content

Microsoft Silverlight

Answered Question Using HttpWebRequest from loca file system to the web.RSS Feed

(0)

edward_494
edward_494

Member

Member

149 points

86 Posts

Using HttpWebRequest from loca file system to the web.

I just read that using a WebClient on a locally running application does not work when reaching out to the web via http.

Since im testing as i develop here i need a way to test services out there on the internet while my project is running (debug) on my local file system.

Add yes, i did place the clientaccesspolicy.xml in my web project, which didn't help.

So what are my options?

 

Since i can't use WebClient i am now resorting to useing HttpWebRequest and HttpWebResponse. Is there anything else i can do to retrieve data from the internet while testing locally on my machine ( i am avoiding web service or wcf calls).

Thanks!

Bill Reiss
Bill Reiss

Contributor

Contributor

4836 points

917 Posts

Silverlight MVP

Re: Using HttpWebRequest from loca file system to the web.

Add a web project to your solution, and then add the Silverlight Application to that web project using the Silverlight Applications section of the web project's property pages. This will generate test pages for you in the web project and the web project can then run in "Cassini", Visual Studio's built in web server. This will allow you to hit resources on the internet.


Bill Reiss, Coauthor of Hello! Silverlight 3
My blog (rss feed)

edward_494
edward_494

Member

Member

149 points

86 Posts

Re: Using HttpWebRequest from loca file system to the web.

Thanks.

Do you mean add a new Web Application project to my solution?

How to enable Cassini mode?

Bill Reiss
Bill Reiss

Contributor

Contributor

4836 points

917 Posts

Silverlight MVP

Re: Using HttpWebRequest from loca file system to the web.

Yes, I think it defaults to Cassini? If not you can go to the property pages for the web application project and on the Web tab select "Use Visual Studio Development Server".


Bill Reiss, Coauthor of Hello! Silverlight 3
My blog (rss feed)

edward_494
edward_494

Member

Member

149 points

86 Posts

Re: Using HttpWebRequest from loca file system to the web.

I confirm that i have the cassini dev server, and that is what im running now to run my application.

However, im still seeing the same thing.

My specific error occurs when i try to call the EndGetResponse().

The exception is: Operation is not valid due to the current state of the object.

Stacetrace:

at System.NetAsyncHelper.BeginOnUI

at System.NetBrowserHttpWebRequest.EndGetResponse

at ResponseCallback

edward_494
edward_494

Member

Member

149 points

86 Posts

Re: Using HttpWebRequest from loca file system to the web.

//////////////////////////////////////////////// Web Request /////////////////////////////////////////////////////// 

HttpWebRequest webRequest = null;

try

{

webRequest = (
HttpWebRequest)WebRequest.Create(_uri);

webRequest.Method = "GET";

webRequest.ContentType = "text/html";

webRequest.BeginGetResponse(ResponseCallback, webRequest);

}

catch (Exception ex)

{

}

/////////////////////////////////////////////////////////////// Response Callback ///////////////////////////////////////////////////////////////// 

static void ResponseCallback(IAsyncResult result)

{

HttpWebRequest request = (HttpWebRequest)result.AsyncState;

StreamReader sr = null;

HttpWebResponse response = null;if (request != null)

{

//if (request.HaveResponse)

{

try

{

response = (
HttpWebResponse)request.EndGetResponse(result);

sr = new StreamReader(response.GetResponseStream());

if (sr != null)

{

string data = sr.ReadToEnd();if (!string.IsNullOrEmpty(data))

{

// parse by ','

data = data.Replace("\"", "");

string[] quoteData = data.Split(new char[] { ',' });

// set data members

_lastTraded = float.Parse(quoteData[0]);

}

}

}

catch (Exception ex)

{

}

finally

{

if (sr != null)

{

sr.Dispose();

sr =
null;

}

}

}

}

}

Bill Reiss
Bill Reiss

Contributor

Contributor

4836 points

917 Posts

Silverlight MVP

Re: Using HttpWebRequest from loca file system to the web.

Ok first make sure that the url is the browser says http://localhost...etc and not C:\ or file://

Then download and install Nikhil's web development helper at http://projects.nikhilk.net/WebDevHelper/ and enable logging

See what requests are being made and make sure they're going where you think they should be going.


Bill Reiss, Coauthor of Hello! Silverlight 3
My blog (rss feed)

edward_494
edward_494

Member

Member

149 points

86 Posts

Re: Using HttpWebRequest from loca file system to the web.

 Interesting.

The url is indeed http://locahost:.....

 However, when i use Fiddler to view http traffic, i see that there is nothing going on!

MY app runs thru an infinite loop. So i should be seeing this webrequest every 3 seconds... yet nothing!

 

Bill Reiss
Bill Reiss

Contributor

Contributor

4836 points

917 Posts

Silverlight MVP

Re: Using HttpWebRequest from loca file system to the web.

Fiddler doesn't trap localhost traffic by default, that's why I recommended Nihkil's WebDevHelper.


Bill Reiss, Coauthor of Hello! Silverlight 3
My blog (rss feed)

edward_494
edward_494

Member

Member

149 points

86 Posts

Re: Using HttpWebRequest from loca file system to the web.

 Now im just confused.

I used the Web Dev Helper to view http logs while my app runs.

Still nothing! i dont see any logs at all.

 

edward_494
edward_494

Member

Member

149 points

86 Posts

Re: Using HttpWebRequest from loca file system to the web.

 Oops! I forgot to mention why i was getting that error.

When i check my callback method from the BeginGetResponse call, i notice that the HasResponse property is false.

This is never true. Now if i then proceed to call the EndGetResponse method, then i receive the invalid state error, which i believe is related.

 

So... i guess the REAL question is, why is my response not being returned?

I currently have my app being run from the Web Application project (rather than the web site) that you suggested. Do I still need to include the crossaccesspolicy.xml?

 I noticed in Fiddler i can see the policy file for Yahoo (: policyref="http://p3p.yahoo.com/w3c/p3p.xml  ). Do I need to specify this somewhere???

edward_494
edward_494

Member

Member

149 points

86 Posts

Re: Using HttpWebRequest from loca file system to the web.

 OK, i think my problem is that i simply can't an outside call from within my silverlight application.

So my solution (in theory) is to create a webmethod that will retrieve this for me. this web service, for convenience, will reside in the same domain as my app.

Qbus
Qbus

Member

Member

607 points

269 Posts

Re: Re: Using HttpWebRequest from loca file system to the web.

I see you have two

catch (Exception ex)

{

}

Why is this empty? In my coding world, it's only in VERY rare scenarios that you need to have an empty catch. What you are doing here, it hiding errors! Yes, it's good to have a applciation that doesn't crash, but what you are doing here, is that you are hiding errors from you and your application, which is very bad.

You never discover if anything of this fails... and the fact that you have placed the try/catch shows that you expect it to fail, so we not look at the error?

Maybe if you write out these exceptions, they would help you figure out where your problem were?

 

--------------------------
Please mark the post as answered if this answers your question
http://www.laumania.net

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

Re: Using HttpWebRequest from loca file system to the web.

Hello, why do you set a ContentType? It has no use in a GET operation. From the document:

This header cannot be set on a GET method. If this header is set, a ProtocolViolationException is thrown.

Remove ContentType, and it should work fine.

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities