Sending POST data
I'm trying to connect with HTTP server and send some date. My code looks like ->
public MainPage()
{
InitializeComponent();
WebClient client = new WebClient();
Uri uri = new Uri("http://google.pl");
string data = "Time = 12:00am temperature = 50";
client.UploadStringCompleted += new UploadStringCompletedEventHandler (UploadStringCallback2);
client.UploadStringAsync(uri, data);
}
private static void UploadStringCallback2(Object sender, UploadStringCompletedEventArgs e)
{
string reply = (string)e.Result;
Console.WriteLine(reply);
}
I receive exception "The remote server returned an error: NotFound." My debug windows looks like
A first chance exception of type 'System.Net.WebException' occurred in System.Windows.dll A first chance exception of type 'System.Net.WebException' occurred in System.Windows.dll A first chance exception of type 'System.Net.WebException' occurred in System.dll
Please help! PS: Ive got installed 7.1 SDK Beta but it should run on 7.0 emulator (Target windows phone verison is WP7).
EDIT:
Now code looks like
Uri uri = new Uri("MY SITE");
string data = "text=dupa";
//client.Encoding = System.Text.Encoding.UTF8;
var headers = new WebHeaderCollection();
headers[0] = " User-Agent: CERN-LineMode/2.15 libwww/2.17b3";
client.Headers = headers;
client.UploadStringCompleted += new UploadStringCompletedEventHandler(UploadStringCallback2);
client.UploadStringAsync(uri, data);
开发者_C百科
It connects to my PHP script
<?php
print_r($_POST);
print_r($_SERVER[HTTP_USER_AGENT]);
?>
But response is like
Array
(
)
NativeHost
Thanks in advance for your help :)
EDIT: OK, i figured it out ;) Everything is working ;)
Google doesn't accept a POST request. I don't know about WP7 but if you create a simple console app with the same code you get the exception that a post method's not allowed.
The WebClient class uses GET method internally, you gotta use the HttpWebRequest and HttpWebResponse classes. Here is a small snippet
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.foo.com");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string postData = "parameter=value";
// Getting the request stream.
request.BeginGetRequestStream
(result =>
{
// Sending the request.
using (var requestStream = request.EndGetRequestStream(result))
{
using (StreamWriter writer = new StreamWriter(requestStream))
{
writer.Write(postData);
writer.Flush();
}
}
// Getting the response.
request.BeginGetResponse(responseResult =>
{
var webResponse = request.EndGetResponse(responseResult);
using (var responseStream = webResponse.GetResponseStream() )
{
using (var streamReader = new StreamReader(responseStream))
{
var result = streamReader.ReadToEnd();
}
}
}, null);
}, null);
Try out this class which simplifies HTTP calls: https://mytoolkit.codeplex.com/wikipage?title=Http (Link is to my codeplex project)
Usage:
var request = new PostRequest("http://myurl.ch");
request.Data.Add("name", "myname");
request.Data.Add("email", "myemail);
Http.Post(request, OnSendCompleted, Deployment.Current.Dispatcher);
OnSendCompleted
is a method which is called after the HTTP call has finished. With this class it's also easy to send files.
精彩评论