使用indy的idhttp进行Post数据的时候,服务器收不到东西

qdh2002 2004-08-18 04:34:37
post2.asp:

response.Write ("form1="&request.Form ("form1"))


C程序:

TIdMultiPartFormDataStream* tData=new TIdMultiPartFormDataStream();
TStringStream* ss=new TStringStream("");


tData->AddFormField("form1","form1data");

tData->Position=0;

Form1->IdHTTP1->Request->ContentType=tData->RequestContentType;

Memo1->Lines->Add(Form1->IdHTTP1->Post("/post2.asp",tData));

ss->Seek(0, soFromBeginning);
Memo1->Lines->Add(ss->DataString);

delete ss;
delete tData;
Form1->IdHTTP1->Disconnect();




//////////

结果运行以后。显示form1=空,asp并没有收到东西,请问如何解决?
因为数据量可能会很大,所以估计不能使用 url?字段=数据&.... 这种方式。。。。。
...全文
579 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kandy 2004-11-21
  • 打赏
  • 举报
回复
不是服务器收不到东西,是你用的方法不对。
首先你是想要结果,而不是在上传图片或者附件等二进制数据。应使用
procedure Post(URL: string; const Source: TStrings; const AResponseContent: TStream); overload;//摘自帮助-》Tidhttp。

其次使用Tidhttp post数据的时候需要指明ContentType,如果不指明的话你依然得不到想要的结果。
下边摘自帮助-》Tidhttp

ContentType contains the value for the HTTP header "Content-Type:". ContentType is a String property that specifies the content type for requests to and responses from an HTTP server. For forms Request.Content Type specifies the type of data being set with the form. Response.ContentType specifies the type of data the server will return.
ContentType refers to the media type or MIME type used for HTTP responses and requests. They are generally supplied in HTTP headers in the following forms:

text/html; charset=ISO-8859-4
text/plain
text/xml
application/x-www-form-urlencoded
multipart/form-data


For forms, the ContentType is normally "application/x-www-form-urlencoded". However the content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.

既然用POST数据那我们应该使用"application/x-www-form-urlencoded"


下边是根据帮助改写的结果范例:
-------A部分--------------post.asp
<%
response.write request("test")
%>
-------B部分--------------BCB中的代码
TStringList* request=new TStringList();
TStringStream* response=new TStringStream("");
request->Add("test=中文测试");
try{
IdHTTP1->Request->ContentType="application/x-www-form-urlencoded";//此句必须写上,否则不返回结果。
IdHTTP1->Post("http://X.X.X.X/post.asp",request,response);
ShowMessage(response->DataString);

}
__finally{
delete request;
delete response;
}


-------------综上:很多问题Help,其实帮助是很好的书。
lihongxing2002 2004-11-09
  • 打赏
  • 举报
回复

604

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder VCL组件使用和开发
社区管理员
  • VCL组件使用和开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧