Httpclient PostAsync返回404.web服务器接收到的所有帖子数据都是完整有序的

本文关键字:数据 PostAsync 服务器 web Httpclient 返回 | 更新日期: 2023-09-27 18:02:43

这是我所拥有的

using Windows.Web.Http;
using Windows.Web.Http.Headers;
public async static Task<bool> FormPost(List<KeyValuePair<string, string>> varvaluepair, string hosturl)
{
    try
    {
        Uri cURI;
        if (Uri.TryCreate(hosturl, UriKind.Absolute, out cURI) && (cURI.Scheme == "http" || cURI.Scheme == "https"))
        {
            var client = new HttpClient();
            client.DefaultRequestHeaders.UserAgent.ParseAdd("ie");
            client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
            var content = new HttpFormUrlEncodedContent(varvaluepair);
            content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/x-www-form-urlencoded");
            var response = await client.PostAsync(cURI, content);
            if (response.IsSuccessStatusCode)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    catch (Exception ex)
    {
        return false;

这是我从响应中得到的:

{StatusCode: 404, ReasonPhrase: 'Not Found', Version: 2, Content: Windows.Web.Http.HttpStreamContent, Headers:
{
  Connection: keep-alive
  Server: nginx admin
  Date: Sun, 11 May 2014 15:46:32 GMT
}{
  Content-Length: 335
  Content-Type: text/html; charset=iso-8859-1
}}  Windows.Web.Http.HttpResponseMessage

我的主机收到提交值,一切正常,除了404的响应和函数返回一个假而不是真。

我是这样发送的:

string tmpAddress = "http://www.somewhere.com";
var tmpData = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>("Name", "Jibah xxxxxxxxxxxx"),
    new KeyValuePair<string, string>("Email", "jibah.kxxxxx@xxxx.com"),
    new KeyValuePair<string, string>("Feedback", "Test submission to see the response"),
    new KeyValuePair<string, string>("form_tools_form_id", "225")
};
bool tmpResult = await FormPost(tmpData, tmpAddress);

和Formtool网站的结果(我不能发布图像)图像捕获

请原谅我的英语。谢谢。

Httpclient PostAsync返回404.web服务器接收到的所有帖子数据都是完整有序的

我不确定这是正确的方式来显示我的发现(有人请纠正我,纠正这个答案)

不在c#代码中。response不是PostAsync发送的,而是被调用的服务发送的。该服务在接收但返回不存在的重定向页面时工作正常。这是Formtool的问题

在Formtool中有一个设置,当Formtool提交和接收表单时返回一个重定向URL。在Formtool中设置的重定向URL确实是一个不存在的页面,因此出现了404.