Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Using HttpWebRequest from loca file system to the web.
13 replies. Latest Post by Yi-Lun Luo - MSFT on October 14, 2008.
(0)
edward_494
Member
149 points
86 Posts
10-09-2008 1:33 PM |
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
Contributor
4836 points
917 Posts
10-09-2008 1:38 PM |
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.
10-09-2008 1:52 PM |
Thanks.
Do you mean add a new Web Application project to my solution?
How to enable Cassini mode?
10-09-2008 2:03 PM |
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".
10-09-2008 3:06 PM |
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
10-09-2008 3:09 PM |
//////////////////////////////////////////////// Web Request /////////////////////////////////////////////////////// HttpWebRequest webRequest = null; try
//////////////////////////////////////////////// Web Request ///////////////////////////////////////////////////////
{
webRequest.Method =
webRequest.BeginGetResponse(ResponseCallback, webRequest);
}
/////////////////////////////////////////////////////////////// Response Callback /////////////////////////////////////////////////////////////////
sr =
sr.Dispose();
10-09-2008 3:11 PM |
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.
10-09-2008 3:20 PM |
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!
10-09-2008 3:25 PM |
Fiddler doesn't trap localhost traffic by default, that's why I recommended Nihkil's WebDevHelper.
10-09-2008 3:37 PM |
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.
10-09-2008 3:53 PM |
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???
10-09-2008 3:59 PM |
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
607 points
269 Posts
10-09-2008 4:31 PM |
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?
Yi-Lun L...
All-Star
25052 points
2,747 Posts
10-14-2008 12:37 AM |
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.