J'essaie d'envoyer une requête POST à un script PHP ("http: // mywebsite/index/test /" dans ce cas) une fois qu'un bouton est cliqué. J'utilise la méthode de WebClient UploadStringAsync pour cela, mais il ne semble pas fonctionner:UploadStringAsync ne fonctionne pas
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>[email protected]</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;
}
}
}
Je sais que le script PHP est pas appelé car il écrit un fichier avec des informations de demande lors de l'exécution. Cependant, je ne reçois aucune erreur ou remarque dans Visual Studio et je ne suis pas sûr de ce qui pourrait être le problème.
EDIT:
J'utilise Silverlight 4, IIS7, PHP 5.3.3.
EDIT 2:
Ok, j'ai réussi à obtenir cette erreur en affichant 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)
Êtes-vous sûr que 'button1_Click' est réellement appelé? – dtb
Le WebClient appelle-t-il jamais 'client_UploadStringCompleted'? – dtb
@dtb Oui, il appelle la méthode client_UploadStringCompleted. J'ai mis HtmlPage.Window.Alert ("aaa"); là et je reçois la fenêtre d'alerte contextuelle. –