使用WebClient从ASP.NET Web服务错误中下载字符串

时间:2012-04-30 02:10:08

标签: asp.net silverlight web-services webclient

我有一个简单的ASP.NET Web服务:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace WebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string Get()
        {
            member mem = new member();
            mem.hello = "hello hello hello";
            mem.kalon = "fafasdf";
            mem.name = "wfasdfawe";
            return new JavaScriptSerializer().Serialize(mem);
        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }

    public class member
    {
        public string hello { get; set; }
        public string name { get; set; }
        public string kalon { get; set; }
    }
}

我正面临使用WebClient从SilverLight检索字符串的问题。你能开导我吗?我使用下面的代码从WebService获取字符串。

WebClient wc = new WebClient();

public MainPage()
{
    InitializeComponent();
    wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    Uri uri = new Uri("http://www.MyWeb.com/Service1.axms/Get");
    wc.DownloadStringAsync(uri);
}

void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    MessageBox.Show(e.Error.ToString());
    MessageBox.Show(e.Result);
}

我收到错误:

System.Security.SecurityException ---&gt; System.Security.SecurityException:安全性错误。    在System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)    在System.Net.Browser.BrowserHttpWebRequest。&lt;&gt; c_ DisplayClass5.b _4(Object sendState)    在System.Net.Browser.AsyncHelper。&lt;&gt; c_ DisplayClass4.b _1(Object sendState)    ---内部异常堆栈跟踪结束---    在System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,Object state)    在System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)    在System.Net.WebClient.GetWebResponse(WebRequest请求,IAsyncResult结果)    在System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult结果)

我也试过这个,仍然得到同样的错误:

Uri uri = new Uri("http://www.MyWeb.com/Service1.axms?op=Get");
wc.DownloadStringAsync(uri);

另外1个问题: 这样的网址是否正确获取字符串?

http://www.MyWeb.com/Service1.axms?op=Get

http://www.MyWeb.com/Service1.axms/Get

0 个答案:

没有答案