Im kind of stuck with a HttpWebRequest while sending post values and i dont know what the problem is.
Hopefully anyone is able to help me with this.
this is my code
private async void loggingIn(string url, string postdata)
{
if (NetworkInterface.GetIsNetworkAvailable())
{
try
{
var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
request.Method = "POST";
byte[] data = Encoding.UTF8.GetBytes(postdata);
request.ContentLength = data.Length;
using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request))
{
await requestStream.WriteAsync(data, 0, data.Length);
}
WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request);
var responseStream = responseObject.GetResponseStream();
var sr = new StreamReader(responseStream);
string received = await sr.ReadToEndAsync();
MessageBox.Show(received);
}
catch
{
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string url = "localhost/test.php";
string password = passwordBoxLogin.Password;
string username = usernameBoxLogin.Text;
string postdata = "password=" + password +"&username="+username;
loggingIn(url,postdata);
}
The problem is that my server does not receive any values from the POST request. but i do get a response from the server.
this is what i use to check the code on my php server
<?php
echo $_POST['username'];
echo"-";
echo $_POST['password'];
?>
the only thing i get returned is the -
Thanks in advance for helping me out :)
It just started working without anychanges.
For now case closed!
I modified You Code Like this Hope it's definitely Work for You
private void Button_Click(object sender, RoutedEventArgs e)
{
string url = "localhost/test.php";
string password = passwordBoxLogin.Password;
string username = usernameBoxLogin.Text;
string postdata = "password=" + password +"&username="+username;
bool isNetwork = NetworkInterface.GetIsNetworkAvailable();
if (!isNetwork)
{
}
else
{
try
{
WebClient webClient = new WebClient();
webClient.Headers["Content-Type"] = "application/json"; // Your Application Header Content-Type
webClient.Encoding = Encoding.UTF8;
webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(proxy_UploadStringCompleted);
webClient.UploadStringAsync(new Uri(url ), "POST", postdata ,null);
}
catch
{
}
}
}
private void proxy_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
var response = e.Result;
}
Try this. it may help you.
private async void loggingIn(string url, string postdata)
{
if (NetworkInterface.GetIsNetworkAvailable())
{
try
{
var request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.Accept = "application/json";
request.ContentType = "application/json";
request.BeginGetRequestStream(loginRequest, request);
}
catch(Exception ex)
{
}
}
private void loginRequest(IAsyncResult asyncResult)
{
string postdata = "password=" + password +"&username="+username;
UTF8Encoding encoding = new UTF8Encoding();
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
Stream _body = request.EndGetRequestStream(asyncResult);
byte[] formBytes = encoding.GetBytes(postdata);
_body.Write(formBytes, 0, formBytes.Length);
_body.Close();
request.BeginGetResponse(loginResponse, request);
}
private void loginResponse(IAsyncResult asyncResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
HttpWebResponse httpResponse = (HttpWebResponse)response;
using (Stream data = response.GetResponseStream())
using (var reader = new StreamReader(data))
{
string received = await reader.ReadToEndAsync();
MessageBox.Show(received);
}
}
catch(Exception ex)
{
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.