我有一个简单的 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 ---> System.Security.SecurityException:安全错误。在 System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) 在 System.Net.Browser.BrowserHttpWebRequest.<>c_ DisplayClass5.b _4(Object sendState) 在 System.Net.Browser.AsyncHelper.<>c_ DisplayClass4.b _1 (Object sendState) --- 内部异常堆栈跟踪结束 --- 在 System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在 System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) 在 System. System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult 结果)处的 Net.WebClient.GetWebResponse(WebRequest 请求,IAsyncResult 结果)
我也试过了,还是一样的错误:
Uri uri = new Uri("http://www.MyWeb.com/Service1.axms?op=Get");
wc.DownloadStringAsync(uri);
还有 1 个问题:这样的 url 是否正确获取字符串?
http://www.MyWeb.com/Service1.axms?op=Get
http://www.MyWeb.com/Service1.axms/Get