Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send an HTTP POST request with C#

I'm try to Send Data Using the WebRequest with POST But my problem is No data has be streamed to the server.

string user = textBox1.Text;
string password = textBox2.Text;  

ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "username" + user + "&password" + password;
byte[] data = encoding.GetBytes(postData);

WebRequest request = WebRequest.Create("http://localhost/s/test3.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();

WebResponse response = request.GetResponse();
stream = response.GetResponseStream();

StreamReader sr99 = new StreamReader(stream);
MessageBox.Show(sr99.ReadToEnd());

sr99.Close();
stream.Close();

here the result

like image 298
abdallah Avatar asked Apr 19 '16 19:04

abdallah


People also ask

What is HTTP POST example?

HTTP works as a request-response protocol between a client and a server in a format that both HTTP clients and servers can understand. For example, when a user uploads a document to the server, the browser sends an HTTP POST request and includes the document in the body of the POST message.

What is HTTP request method == POST?

POST is an HTTP method designed to send data to the server from an HTTP client. The HTTP POST method requests the web server accept the data enclosed in the body of the POST message. HTTP POST method is often used when submitting login or contact forms or uploading files and images to the server.

How do I make a Web request in C++?

C++ does not provide any way to do it directly. It would entirely depend on what platforms and libraries that you have. At worst case, you can use the boost::asio library to establish a TCP connection, send the HTTP headers (RFC 2616), and parse the responses directly.

Can HTTP POST have response body?

Yes you can, and the specification is clear about what you can do and how to do it: The action performed by the POST method might not result in a resource that can be identified by a URI.


1 Answers

It's because you need to assign your posted parameters with the = equal sign:

byte[] data = Encoding.ASCII.GetBytes(
    $"username={user}&password={password}");

WebRequest request = WebRequest.Create("http://localhost/s/test3.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

string responseContent = null;

using (WebResponse response = request.GetResponse())
{
    using (Stream stream = response.GetResponseStream())
    {
        using (StreamReader sr99 = new StreamReader(stream))
        {
            responseContent = sr99.ReadToEnd();
        }
    }
}

MessageBox.Show(responseContent);

See the username= and &password= in post data formatting.

You can test it on this fiddle.

Edit :

It seems that your PHP script has parameters named diffently than those used in your question.

like image 69
Fabien ESCOFFIER Avatar answered Oct 12 '22 23:10

Fabien ESCOFFIER