Skip to main content

Microsoft Silverlight

Answered Question webClient DownloadStringAsync failedRSS Feed

(0)

work2gs
work2gs

Member

Member

58 points

79 Posts

webClient DownloadStringAsync failed

Hi

I successfully send http request from winform C# application (see code1)
but from silverlight (see code2) I obtain an error.(see error)

Peharps the problem is that my server is https and the remote is also https.

How I modify my server to be http ???

Thanks in advance
Fred

code1 (Winform C# succeeded)

private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                WebClient client = new WebClient();
                // Add a user agent header in case the requested URI contains a query.
                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                client.QueryString.Add("user", "AAA");
                client.QueryString.Add("password", "1111111");
                client.QueryString.Add("api_id", "3126312");
                client.QueryString.Add("to", "972545665417");
                client.QueryString.Add("text", "This is an example message");
                string baseurl = "http://api.clickatell.com/http/sendmsg";
                Stream data = client.OpenRead(baseurl);
                StreamReader reader = new StreamReader(data);
                string s = reader.ReadToEnd();
                data.Close();
                reader.Close();
                MessageBox.Show(s);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

code2 (silverlight failed)

private void sendSMS()
        {
            WebClient client = new WebClient ();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
           
            string baseurl ="http://api.clickatell.com/http/sendmsg";
            baseurl +="?user=AAA";
            baseurl +="&password=1111111";
            baseurl +="&api_id=3126312";
            baseurl +="&to=972545665417";
            baseurl +="&text=This+is+an+example+message";

            Uri uri = new Uri(baseurl, UriKind.Absolute);
            client.DownloadStringAsync(new Uri(baseurl));
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
           
            if (e.Error == null)
            {
                string s = e.Result.ToString();
                Debugger.Log(0, "debug", "s=" + s+ "\r\n");
            }
            else
                Debugger.Log(0, "debug", "error=" + e.Error.ToString()+ "\r\n");
        }

error
baseurl=http://api.clickatell.com/http/sendmsg?user=AAA&password=1111111&api_id=3126312&to=972545665417&text=This+is+an+example+message
'iexplore.exe' (Silverlight): Loaded 'C:\Program Files\Microsoft Silverlight\2.0.31005.0\en-US\mscorlib.debug.resources.dll'

error=System.Security.SecurityException ---> System.Security.SecurityException: Security error.
   at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
   at System.Net.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
   at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)

robhouweling
robhouwe...

Contributor

Contributor

3184 points

548 Posts

Silverlight MVP

Re: webClient DownloadStringAsync failed

Have you created a crossdomain file on your server? The error you're getting looks like a crossdomain security issue.

(If this has answered your question, please click on mark as answer on this post)



Cheers!

Rob Houweling





My blog

work2gs
work2gs

Member

Member

58 points

79 Posts

Re: webClient DownloadStringAsync failed

 Thanks for your response

I define a clientaccesspolicy.xml in my web application that contains this 

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-domain-access>
    <allow-from http-request-headers="*">
      <domain uri="*"/>
    </allow-from>
    <grant-to>
      <resource path="/" include-subpaths="true"/>
    </grant-to>
  </cross-domain-access>
</access-policy>

without success

Could you tell me what is missing ?

thanks

Fred 

Evs_
Evs_

Member

Member

58 points

71 Posts

Re: Re: webClient DownloadStringAsync failed

 try to install fiddler to check if someone is wrong ;)...have a look to my old post: http://silverlight.net/forums/t/39985.aspx

 

work2gs
work2gs

Member

Member

58 points

79 Posts

Re: Re: webClient DownloadStringAsync failed

Hi

Do you mean that the remote server (api.clickatell.com)

I try to access have to be the clientaccesspolicy.xml existing ?

This is what I obtain from Fiddler

 GET http://api.clickatell.com/clientaccesspolicy.xml
404 Not Found

 .REQUEST { font: 8pt Courier New; color: blue;} .RESPONSE { font: 8pt Courier New; color: green;}GET /clientaccesspolicy.xml HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Host: api.clickatell.com
Proxy-Connection: Keep-Alive

HTTP/1.1 404 Not Found
Via: 1.1 Address Is Hidden
Connection: Keep-Alive
Proxy-Connection: Keep-Alive
Content-Length: 304
Date: Sun, 26 Oct 2008 08:57:58 GMT
Content-Type: text/html; charset=iso-8859-1
Server: Apache/2.2.3 (Red Hat)
Keep-Alive: timeout=10, max=50

 

Thanks

Fred

Evs_
Evs_

Member

Member

58 points

71 Posts

Re: Re: Re: webClient DownloadStringAsync failed

 HI, as you can see, clientpolicy.xml  must be on the server( your application tries to download it but it can't find)....

So you have to put it on the server, not in your application ;)

Mark this post  as answer if it solves your problem ;)

Bye 

pbromberg
pbromberg

Contributor

Contributor

2128 points

370 Posts

Answered Question

Re: webClient DownloadStringAsync failed

Have a look at their crossdomain.xml policy file and you'll see why Siliverlight will not let you do this:

 

http://api.clickatell.com/crossdomain.xml

 

What you need to do is create your own service on the same server that your Silverlight App is served from, and have that service make the call.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities