IdHTTP的POST请求 [英] POST request with IdHTTP

查看:392
本文介绍了IdHTTP的POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用组件IdHTTP POST方法填写表单,我的代码是这样的:

Hi I'm trying to fill in a form using the component IdHTTP POST method, the code that I have is this:

var
  par2: TIdMultiPartFormDataStream;
  rta: string;

begin

    par2 := TIdMultiPartFormDataStream.Create;
    par2.AddFormField('ipaddress', ip.text);
    par2.AddFormField('submit', 'Submit');

    rta := idhttp1.Post
      ('http://www.melissadata.com/lookups/iplocation.asp?ipaddress=', par2);

    memo.Lines.Add(rta);

end;

表格的代码是这样:

<form method="post" action="iplocation.asp">
<table class="Tableresultborderblue" align="right" border="0" cellpadding="0" cellspacing="0" width="300">
<tbody><tr><td align="center"><span style="font-size:12px;">Your IP Address: 181.92.20.173</span></td></tr>
<tr><td align="center" height="35px"><strong>Enter an IP address</strong></td></tr>
<tr><td align="center"><input id="ipaddress" name="ipaddress" value="" class="inputoff" type="text"></td></tr>
<tr><td height="10"></td></tr>
<tr><td align="center" height="45px"><input title="Click to process Address" class="btn" value="Submit" type="submit"></td></tr>
<tr><td height="10"></td></tr>
</tbody></table>   
</form>

问题是我没有返回适当的回复表格,这使我回想的是空表格好像他错了

The problem is that I do not return the appropriate response form, which brings me back is the empty form as if he had it all wrong

我在做什么错了?

推荐答案

您正在使用 TIdMultipartFormDataStream 提交Webform数据,该表单使用 multipart / form-data 发布数据格式。但是,HTML < form> 标记没有 enctype = multipart / form-data 属性,因此服务器不希望使用该格式。期望使用默认的 application / x-www-webform-urlencoded 格式提交数据。这是通过使用 TStrings 对象发布数据来实现的。

You are submitting the webform data using a TIdMultipartFormDataStream, which posts the data using the multipart/form-data format. However, the HTML <form> tag does not have an enctype=multipart/form-data attribute, so the server is not expecting that format. It is expecting the data to be submitted using the default application/x-www-webform-urlencoded format instead. That is accomplished by posting the data using a TStrings object instead.

以下代码对我来说很好,它收到包含IP地址查找结果的HTML响应(接收后必须解析):

The following code works fine for me, it receives an HTML response that includes the results of the IP address lookup (which you would have to parse after receiving):

var
  PostData: TStringList;
  rta: string;
begin
  PostData := TStringList.Create;
  try
    PostData.Add('ipaddress='+ip.Text);
    rta := IdHTTP1.Post('http://www.melissadata.com/lookups/iplocation.asp', PostData);
  finally
    PostData.Free;
  end;

  Memo1.Lines.Text := rta;
end;

话虽如此,从政治上说是正确的还有两个因素考虑。如果使用Wireshark之​​类的数据包嗅探器查看普通Web浏览器提交的内容,则会注意到:

That being said, to be politically correct there are two other factors to consider. If you use a packet sniffer like Wireshark to see what a normal web browser submits, you will notice that:


  1. Webform提交中的 Referer 标头,以使服务器知道提交的来源。您将省略该标头。有时,网络服务器会验证 Referer 以确保请求实际上来自其自己的站点而不是其他地方,因此您应该提供一个 Referer

  1. there is a Referer header on the webform submission, to let the server know where the submission originated from. You are omitting that header. Sometimes web servers validate the Referer to make sure a request is actually coming from their own site and not somewhere else, so you should provide a Referer when appropriate.

服务器发送带有初始HTML的cookie,然后将其与Webform提交一起发送回服务器。有时,Web服务器需要这些cookie,以确保客户端在提交Web表单数据之前已访问原始站点。因此,您应该下载初始HTML,以使 TIdHTTP 获取任何必要的cookie,以便将其发送回服务器。

the server sends a cookie with the initial HTML, which then gets sent back to the server with the webform submission. Sometimes web servers require those cookies to ensure the client visited the original site before submitting the webform data. So you should download the initial HTML to let TIdHTTP obtain any necessary cookies so it can send them back to the server.

以下代码对我也可以正常工作,收到包含IP地址查找结果的相同HTML响应:

The following code also works fine for me, receives the same HTML response that includes the results of the IP address lookup:

var
  PostData: TStringList;
  rta: string;
begin
  // Get webform HTML and any cookies that go with it
  IdHTTP1.Get('http://www.melissadata.com/lookups/iplocation.asp');

  // now post the webform data back to the server
  PostData := TStringList.Create;
  try
    PostData.Add('ipaddress=23.241.61.8');
    IdHTTP1.Request.Referer := 'http://www.melissadata.com/lookups/iplocation.asp';
    rta := IdHTTP1.Post('http://www.melissadata.com/lookups/iplocation.asp', PostData);
  finally
    PostData.Free;
  end;

  Memo1.Lines.Text := rta;
end;

最后,您正在使用基于HTML的查找服务,这不是一个很好的决定。 HTML是用于呈现供人类使用的数据,而不适合用于机器解析。还有许多其他IP查找服务可用,它们提供了更有效的基于REST的API,以机器可解析的格式(例如XML或JSON)提供结果。您应该认真考虑改用其中一种服务。

Lastly, you are using an HTML-based lookup service, which is not a very good decision. HTML is meant for presenting data for human consumption, not well suited for machine parsing. There are plenty of other IP lookup services available that provide more efficient REST-based APIs for providing results in machine-parsable formats, like XML or JSON. You should seriously consider switching to one of those services instead.

这篇关于IdHTTP的POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆