An HTTP message in a C # console application does not return the same as a browser request

I have a C # console application (.NET 2.0 framework) that is executing an HTTP message using the following code:

StringBuilder postData = new StringBuilder(100);
postData.Append("post.php?");
postData.Append("Key1=");
postData.Append(val1);
postData.Append("&Key2=");
postData.Append(val2);

byte[] dataArray = Encoding.UTF8.GetBytes(postData.ToString());

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://example.com/");
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";

httpRequest.ContentLength = dataArray.Length;
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(dataArray, 0, dataArray.Length);
requestStream.Flush();
requestStream.Close();

HttpWebResponse webResponse = (HttpWebResponse)httpRequest.GetResponse();

if (httpRequest.HaveResponse == true) {
  Stream responseStream = webResponse.GetResponseStream();
  StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
  String responseString = responseReader.ReadToEnd();
}

Outputs from this:
webResponse.ContentLength = -1
webResponse.ContentType = text / html
webResponse.ContentEncoding is empty.

ReplyString is HTML with a header and body.

However, if I put the same URL in the browser ( http://example.com/post.php?Key1=some_value&Key2=some_other_value ), I get a small XML fragment, for example:

<?xml version="1.0" ?>
<RESPONSE RESULT="SUCCESS"/>

HTML-, . ? , HTML. ? , .

+3
7

HTTP- POST, . -, :

postData.Append("post.php?");

. post.php, post.php? . .

:

... WebRequest.Create("http://example.com/");

post.php, ...

... WebRequest.Create("http://example.com/post.php");

, POST GET ing. GET, , .

+9

HTTP, Fiddler , . - , . , , . ( , , -, - .)

+3

.

"User-Agent" "Mozilla...".

, .

+2

POST, PHP, -. , POST, GET. , GET , PHP -, , , - . , GET.

html POST url . , , .

, GET. , .

+1

, GET- POST. URL-, , querystring (, Key1 = some_value & Key2 = some_other_value), GET. , post -, .

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://example.com/?val1=" + val1 + "&val2=" + val2);
httpRequest.Method = "GET";
httpRequest.ContentType = "application/x-www-form-urlencoded";
....

, , , , , , .

+1

, - , WebRequest.

, .

, -, - example.com .

, ContentType "multipart/form-data".

, , , . . , , GET POST.

0

cookie? , , .

0

Source: https://habr.com/ru/post/1703157/


All Articles