none
WebClient not working

    Question

  • I have a Silverlight control that exposes a couple of methods. I also have a JavaScript bridge that interacts between Silverlight and the server. When the server needs to invalidate the view in the control it sends a "signal" to the control which in turns creates a WebClient instance and sends an asynchronus request to the server for new data like this:

    WebClient c = new WebClient();
    Uri uri = new Uri(url);            
    c.OpenReadCompleted += wc_OpenReadCompleted;            
    c.OpenReadAsync(uri);

    However, the request is not being processed. I do not get an error either, so not sure what is going on. I added both a clientaccesspolocy.xml file and a crossdomain.xml file to my server and did not work. Any ideas?

    Tuesday, May 22, 2012 5:24 PM

Answers

  • Thanks everyone for all your replies. It ended up being a setting in IE8. The"Temporary Internet Files" was set to "Automatically" so it was storing the data in the cache and never refreshing. It has to be setup as "Every time I visit the webpage" or a version number needs to be added to the URL and it works.

    Friday, May 25, 2012 1:59 PM

All replies

  • What error are you getting?  Is it a "NotFound" error?  If so, it may be a service issue at the requested url.

    Wednesday, May 23, 2012 10:55 AM
  • sends an asynchronus request to the server

    To which server do you send request? If it is server which host your SL application, so you can use WCF services or WebClient with relative urls and without any clientaccesspolocy.xml file and a crossdomain.xml. If it is third company server, you can use HttpWebRequest like this:

    IWebRequestCreate creator = WebRequestCreator.ClientHttp;
    
    WebRequest.RegisterPrefix("http://", creator);
    WebRequest.RegisterPrefix("https://", creator);
    
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.server.com/");
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.CookieContainer = cookieContainer;
    
    string formParams = "username=" + Login + "&password=" + Password + "&action=li";
    
    requestContent = Encoding.UTF8.GetBytes(formParams);
    request.ContentLength = requestContent.Length;
    
    request.BeginGetRequestStream(new AsyncCallback(getLogonRequestCallback), request);
    
    ...
    
    private void getLogonRequestCallback(IAsyncResult asynchronousResult)
    {
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
    
    Stream newStream = request.EndGetRequestStream(asynchronousResult);
    newStream.Write(requestContent, 0, requestContent.Length);
    newStream.Close();
    
    request.BeginGetResponse(new AsyncCallback(getLogonResponseCallback), request);
    }
    
    private void getLogonResponseCallback(IAsyncResult asynchronousResult)
    {
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
    
    saveCookie(response.Cookies);
    
    Stream dataStream = response.GetResponseStream();
    StreamReader streamReader = new StreamReader(dataStream, Encoding.UTF8);
    
    ...
    
    }
    

     

     

    Wednesday, May 23, 2012 12:06 PM
  • Thanks for your responses guys. I am not getting an error back which is what puzzles me. I installed Fiddler to see the network activity (we use IE 8 here) and I see the request being posted when the control (or the page) is opened. So, in other words it is invalidated correctly as soon as the control is opened, it requests the data from the server (with WebClient as explained) and the server responds with an XML file which I read, parse and display the data accordingly. After that, everytime the view is invalidated the request is not shown in Fiddler and no error is displayed.

    I had something similar happened to me once while trying to place a request with a very large string in the url which maxed out both the server capacity and IE. In this case that is not happening as my URL is short (yes, I already checked that). The response of the server is with an XML file. Is there any limitations for the file size? Security that I may have to check or plae on my request header or something? It is puzzleling that it works on upload and never again.

    Wednesday, May 23, 2012 4:06 PM
  • Forgot to mention, I did another test using Firefox and it works everytime. So, not sure if this is a browser dependent error or server... it is looking like a browser problem... Does IE has any limitations (or security setting) for this?

    Wednesday, May 23, 2012 4:22 PM
  • IE has limitation for 2048 symbols in URL.

    Wednesday, May 23, 2012 4:47 PM
  • Thanks! No, that is not it. I counted the characters in my URL (exactly thinking about my previous experience) and with a GUID in the URL plus my own request it gets up to 105 characters... not even close to that. 

    Wednesday, May 23, 2012 5:14 PM
  • Is there any special character in your url? Try delete them and check if it works in IE.

    Thursday, May 24, 2012 3:11 AM
  • No I had checked that as well. No special characters. Thanks for your help!

    Thursday, May 24, 2012 11:38 AM
  • If you set a breakpoint in the Completed event handler, what values do you get for e.Result and e.Error (where e is the second parameter of the event handler)?

    Friday, May 25, 2012 10:15 AM
  • Thanks everyone for all your replies. It ended up being a setting in IE8. The"Temporary Internet Files" was set to "Automatically" so it was storing the data in the cache and never refreshing. It has to be setup as "Every time I visit the webpage" or a version number needs to be added to the URL and it works.

    Friday, May 25, 2012 1:59 PM