我正在尝试在单击按钮后向PHP脚本发送POST请求(在本例中为“http:// mywebsite / index / test /”)。我正在使用WebClient的方法UploadStringAsync,但它似乎不起作用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MyWebsite
{
public partial class Home : Page
{
WebClient client = new WebClient();
public Home()
{
InitializeComponent();
client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
}
// Executes when the user navigates to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void button1_Click(object sender, RoutedEventArgs e)
{
// Create the request.
string postRequest = "<entry xmlns='http://www.w3.org/2005/Atom'>"
+ "<title type='text'>New Restaurant</title>"
+ "<content type='xhtml'>"
+ " <div xmlns='http://www.w3.org/1999/xhtml'>"
+ " <p>There is a new Thai restaurant in town!</p>"
+ " <p>I ate there last night and it was <b>fabulous</b>.</p>"
+ " <p>Make sure and check it out!</p>"
+ " </div>"
+ " </content>"
+ " <author>"
+ " <name>Pilar Ackerman</name>"
+ " <email>packerman@contoso.com</email>"
+ " </author>"
+ "</entry>";
// Sent the request to the specified URL.
client.UploadStringAsync(new Uri("http://mywebsite/index/test/",
UriKind.Absolute), postRequest);
}
// Event handler for the UploadStringCompleted event.
void client_UploadStringCompleted(object sender,
UploadStringCompletedEventArgs e)
{
// Output the response.
if (e.Error != null)
textBlock3.Text = e.Error.Message;
else
textBlock3.Text = e.Result;
}
}
}
我知道PHP脚本没有被调用,因为它在执行时写入了带有请求信息的文件。但是我没有在Visual Studio中收到任何错误或通知,我不确定可能是什么问题。
编辑:
我正在使用Silverlight 4,IIS7,PHP 5.3.3。
编辑2:
好的,我设法通过显示e.Error.ToString():
来获得此错误System.Security.SecurityException ---> System.Security.SecurityException: Security error.
at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
--- End of inner exception stack trace ---
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)
答案 0 :(得分:2)
Silverlight是否与Web服务托管在同一个域中?如果不是这种情况,则需要创建clientaccesspolicy.xml文件。
示例:
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="SOAPAction">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
<强>更新强>: 如果您安装Fiddler,您将能够快速确认它是否确实是一个跨网域问题。