Send an HTTP POST request with C# -


i'm try send data using webrequest post problem no data has streamed 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 result

it's because need assign posted parameters = 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 username= , &password= in post data formatting.

you can test on fiddle.

edit :

it seems php script has parameters named diffently used in question.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -