C# 控制台应用程序中的 HTTP Post 不会返回与浏览器请求相同的内容

发布于 2024-07-13 10:10:27 字数 1652 浏览 6 评论 0原文

我有一个 C# 控制台应用程序(.NET 2.0 框架),它使用以下代码执行 HTTP post:

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();
}

其输出是:
webResponse.ContentLength = -1
webResponse.ContentType = 文本/html
webResponse.ContentEncoding 为空

responseString 是带有标题和正文的 HTML。

但是,如果我将相同的 URL 发布到浏览器中 (http://example. com/post.php?Key1=some_value&Key2=some_other_value),我得到一个小的 XML 片段,例如:


没有与应用程序中相同的 HTML。 为什么反应如此不同? 我需要解析 HTML 中没有得到的返回结果。 我是否必须更改在应用程序中发帖的方式? 我无法控制接受帖子的服务器端代码。

I have a C# console app (.NET 2.0 framework) that does an HTTP post 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();
}

The outputs from this are:
webResponse.ContentLength = -1
webResponse.ContentType = text/html
webResponse.ContentEncoding is blank

The responseString is HTML with a title and body.

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

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

with none of the same HTML as in the application. Why are the responses so different? I need to parse the returned result which I am not getting in the HTML. Do I have to change how I do the post in the application? I don't have control over the server side code that accepts the post.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

悲念泪 2024-07-20 10:10:27

如果您确实应该使用 POST HTTP 方法,那么您就犯了一些错误。 首先,这一行:

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

是不正确的。 您想发布 post.php,但不想发布值“post.php”? 到页面。 只需完全删除此行即可。

这一部分:

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

需要添加 post.php ,所以...

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

这再次假设您实际上应该 POST 到指定页面而不是 获取。 如果您应该使用 GET,则已提供的其他答案适用。

If you are indeed supposed to use the POST HTTP method, you have a couple things wrong. First, this line:

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

is incorrect. You want to post to post.php, you don't want post the value "post.php?" to the page. Just remove this line entirely.

This piece:

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

needs post.php added to it, so...

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

Again this is assuming you are actually supposed to be POSTing to the specified page instead of GETing. If you are supposed to be using GET, then the other answers already supplied apply.

樱&纷飞 2024-07-20 10:10:27

您需要获得一个 HTTP 嗅探器工具,例如 Fiddler 并比较从以下位置发送的标头您的应用程序发送给浏览器发送的应用程序。 会有一些不同的东西导致服务器返回不同的响应。 当您调整应用程序以发送浏览器发送的相同内容时,您应该得到相同的响应。 (它可以是用户代理、cookie,任何东西,但肯定有所不同。)

You'll want to get an HTTP sniffer tool like Fiddler and compare the headers that are being sent from your app to the ones being sent by the browser. There will be something different that is causing the server to return a different response. When you tweak your app to send the same thing browser is sending you should get the same response. (It could be user-agent, cookies, anything, but something is surely different.)

你在看孤独的风景 2024-07-20 10:10:27

我过去见过这个。

当您从浏览器运行时,标题中的“User-Agent”是“Mozilla ...”。

当您从程序运行时,它是不同的,并且通常特定于所使用的语言。

I've seen this in the past.

When you run from a browser, the "User-Agent" in the header is "Mozilla ...".

When you run from a program, it's different and generally specific to the language used.

听你说爱我 2024-07-20 10:10:27

我认为您需要使用 GET 请求,而不是 POST。 如果您使用的 url 具有查询字符串值(例如 ?Key1=some_value&Key2=some_other_value),那么它需要 GET。 无需将发布值添加到您的网络请求中,只需将此数据放入查询字符串中即可。

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

因此,当您从应用程序发布数据时,您得到的结果是不同的,因为当服务器端代码无法读取查询字符串中期望的数据时,它会具有不同的输出。

I think you need to use a GET request, instead of POST. If the url you're using has querystring values (like ?Key1=some_value&Key2=some_other_value) then it's expecting a GET. Instead of adding post values to your webrequest, just put this data in the querystring.

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

So, the result you're getting is different when you POST the data from your app because the server-side code has a different output when it can't read the data it's expecting in the querystring.

作妖 2024-07-20 10:10:27

在您的代码中,您指定 POST 方法,该方法将数据发送到 PHP 文件,而不将数据放入网址中。 当你把信息放到地址栏的时候,那不是POST方法,那是GET方法。 这个名字可能令人困惑,但 GET 只是意味着数据通过网址发送到 PHP 文件,而不是在幕后,并不是说它应该获取任何信息。 当您将地址输入浏览器时,它使用的是 GET。

创建一个简单的 html 表单并指定 POST 作为方法,指定您的 url 作为操作。 您将看到信息已发送,但没有出现在地址栏中。

然后执行相同的操作,但指定 GET。 您将在地址栏中看到您发送的信息。

In your code you a specify the POST method which sends the data to the PHP file without putting the data in the web address. When you put the information in the address bar, that is not the POST method, that is the GET method. The name may be confusing, but GET just means that the data is being sent to the PHP file through the web address, instead of behind the scenes, not that it is supposed to get any information. When you put the address in the browser it is using a GET.

Create a simple html form and specify POST as the method and your url as the action. You will see that the information is sent without appearing in the address bar.

Then do the same thing but specify GET. You will see the information you sent in the address bar.

单身狗的梦 2024-07-20 10:10:27

我相信这个问题与您为 WebRequest 设置标头的方式有关。

我见过一些奇怪的情况,尝试通过更改请求中的标头来模拟浏览器会对服务器产生影响。

简而言之,您的控制台应用程序不是 Web 浏览器,并且 example.com 的 Web 服务器期望与浏览器交互。

您还可以考虑将 ContentType 更改为“multipart/form-data”。

我觉得奇怪的是你基本上没有发布任何内容。 这项工作是由查询字符串完成的。 因此,您可能应该使用 GET 而不是 POST。

I believe the problem has something to do with the way your headers are set up for the WebRequest.

I have seen strange cases where attempting to simulate a browser by changing headers in the request makes a difference to the server.

The short answer is that your console application is not a web browser and the web server of example.com is expecting to interact with a browser.

You might also consider changing the ContentType to be "multipart/form-data".

What I find odd is that you are essentially posting nothing. The work is being done by the query string. Therefore, you probably should be using a GET instead of a POST.

不可一世的女人 2024-07-20 10:10:27

表单是否需要 cookie? 这是它在浏览器中工作而不是在控制台应用程序中工作的另一个可能原因。

Is the form expecting a cookie? That is another possible reason why it works in the browser and not from the console app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文