TIdHTTP的Post方法为何速度很慢 ? 查看请求时间图比get()多一个 connect 过程 ?

qq363436899 2014-10-05 12:26:38

Get方法耗时



Post方法耗时 。

上面是在C++builder中使用 TIdHTTP这个控件 来进行 get和 Post 的操作的, 都能正常通信。用浏览器打开同样的网站,get所用时间和浏览器是差不多的 , 但是 Post过程使用的时间比浏览器慢2~3倍,最后发现 Post多了一个 1.Connect 的操作, 多出来的时间就是这个操作占用的 , 请问大神如何优化TIdHTTP 的 post方法 使其没有多余的费时操作 , 能和 Get方法的速度一样快 , 不胜感激 ! (上图是用 抓包工具 HTTP Analyzer V7抓包得到的 )
...全文
650 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq363436899 2014-10-09
  • 打赏
  • 举报
回复
请问有那些区别
bigfog 2014-10-09
  • 打赏
  • 举报
回复
idhttp非常方便,不过,不同版本下,差异很大
qq363436899 2014-10-08
  • 打赏
  • 举报
回复
大神们 求支援 !
sczyq 2014-10-07
  • 打赏
  • 举报
回复
不支持 SSL, 这个就是因为简单, 主要是看POST原理
qq363436899 2014-10-07
  • 打赏
  • 举报
回复
多谢仁兄 ,我是连接的 HTTPS的网站 , 所以要是用 OPSSL ,请问上面这个方法支持 SSL吗
sczyq 2014-10-06
  • 打赏
  • 举报
回复
本哥们写的一个简单的HTTP类, 只有 Get Post Download 三种方法, 如果够用就行了 (TIdHTTP 太多了, 还要拖上一个 IndyProtocolsXXX.bpl) SimpleHTTPProtocol.h

//---------------------------------------------------------------------------
#ifndef SimpleHTTPProtocolH
#define SimpleHTTPProtocolH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <IdBaseComponent.hpp>
#include <IdComponent.hpp>
#include <IdTCPClient.hpp>
#include <IdTCPConnection.hpp>
//---------------------------------------------------------------------------
class TSimpleHTTP : public TIdTCPClientCustom
{
private:
	IIdTextEncoding * FTextEncoding;
	bool __fastcall SendHeader(String AURL, int APostDataLength = 0);
	int __fastcall ReceiveResponse(void);
protected:
	void __fastcall DoOnConnected(void);
public:
	String __fastcall Get(String AURL);
	String __fastcall Post(String AURL, String AData);
	bool __fastcall Download(String AURL, TStream * AStream);

	__fastcall TSimpleHTTP(TComponent * AOwner, IIdTextEncoding * AEncoding = NULL);
	__fastcall ~TSimpleHTTP(void);
};
#endif
SimpleHTTPProtocol.cpp

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "SimpleHTTPProtocol.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma argsused
//---------------------------------------------------------------------------
__fastcall TSimpleHTTP::TSimpleHTTP(TComponent* AOwner,
			IIdTextEncoding * AEncoding) : TIdTCPClientCustom(AOwner)
{
	if (AEncoding)
		FTextEncoding = AEncoding;
	else FTextEncoding = IndyTextEncoding_OSDefault();
}
//---------------------------------------------------------------------------
__fastcall TSimpleHTTP::~TSimpleHTTP(void)
{
	if (Connected())
	{
		IOHandler->CloseGracefully();
		Disconnect(false);
	}
}
//---------------------------------------------------------------------------
bool __fastcall TSimpleHTTP::SendHeader(String AURL, int APostDataLength)
{
	bool LResult = false;
	String LHost, S;
	Word LPort = 80;

	if (AURL.Length() < 8) return LResult;
	if (AURL.UpperCase().Pos("HTTP://") != 1) return LResult;

	AURL.Delete(1, 7);

	if (AURL.Pos("/"))
	{
		LHost = AURL.SubString(1, AURL.Pos("/") - 1);
		AURL.Delete(1, AURL.Pos("/") - 1);
	}
	else
	{
		LHost = AURL;
		AURL = "/";
	}

	int LLength = LHost.Pos(":");
	if (LLength)
	{
		S = LHost;
		S.Delete(1, LLength);
		LPort = S.ToIntDef(80);
		LHost.SetLength(LLength - 1);
	}

	if (!SameText(Host, LHost) || Port != LPort)
	{
		if (Connected()) Disconnect();
		Host = LHost;
		Port = LPort;
	}

	if (!Connected()) Connect();

	if (Connected())
	{
		S = (APostDataLength ? "POST" : "GET");

		IOHandler->WriteLn(S + " " + AURL + " HTTP/1.1");
		IOHandler->WriteLn("Connection: Keep-Alive");
		IOHandler->WriteLn("Content-Type: application/x-www-form-urlencoded");
		if (APostDataLength)
			IOHandler->WriteLn("Content-Length: " + IntToStr(APostDataLength));

		S = LHost;
		if (LPort != 80)
			S += ":" + IntToStr(LPort);

		IOHandler->WriteLn("Host: " + S);
		IOHandler->WriteLn("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		IOHandler->WriteLn("Accept-Encoding: identity");
		IOHandler->WriteLn("User-Agent: Mozilla/3.0 (compatible; Indy Library)");
		IOHandler->WriteLn();
		LResult = true;
	}

	return LResult;
}
//---------------------------------------------------------------------------
int __fastcall TSimpleHTTP::ReceiveResponse(void)
{
	int LResult = 0;
	bool HttpOK = false;
	String S;

	do
	{
		S = IOHandler->ReadLn();

		if (SameText(S.SubString(1, 9), "HTTP/1.1 "))
		{
			HttpOK = S.Pos(" 200 OK");
		}
		else if (SameText(S.SubString(1, 15), "Content-Length:"))
		{
			S.Delete(1, S.Pos(" "));
			LResult = S.ToIntDef(0);
		}
	}
	while (S.Length());

	if (!HttpOK) LResult = 0;

	return LResult;
}
//---------------------------------------------------------------------------
void __fastcall TSimpleHTTP::DoOnConnected(void)
{
	IOHandler->DefStringEncoding = FTextEncoding;
	TIdTCPClientCustom::DoOnConnected();
}
//---------------------------------------------------------------------------
String __fastcall TSimpleHTTP::Get(String AURL)
{
	String LResult;
	if (SendHeader(AURL))
	{
		int LLength = ReceiveResponse();
		if (LLength)
			LResult = IOHandler->ReadString(LLength);
	}
	return LResult;
}
//---------------------------------------------------------------------------
String __fastcall TSimpleHTTP::Post(String AURL, String AData)
{
	String LResult;
	TIdBytes LBytes = FTextEncoding->GetBytes(AData);
	if (SendHeader(AURL, LBytes.Length))
	{
		IOHandler->Write(LBytes, LBytes.Length);
		int LLength = ReceiveResponse();
		if (LLength)
			LResult = IOHandler->ReadString(LLength);
	}
	return LResult;
}
//---------------------------------------------------------------------------
bool __fastcall TSimpleHTTP::Download(String AURL, TStream * AStream)
{
	bool LResult = false;
	AStream->Size = 0;
	if (SendHeader(AURL))
	{
		int LLength = ReceiveResponse();
		if (LLength)
		{
			IOHandler->ReadStream(AStream, LLength, false);
			AStream->Position = 0;
			if (AStream->Size == LLength)
				LResult = true;
		}
	}
	return LResult;
}
//---------------------------------------------------------------------------
sczyq 2014-10-06
  • 打赏
  • 举报
回复
GET 方式: 发送HTTP协议 接收数据 POST 方式: 发送HTTP协议 发送POST数据 接收数据 原理就是这样的, POST 方式 的二次发送, 应该是在同一个连接内完成的,
qq363436899 2014-10-06
  • 打赏
  • 举报
回复
跪求大神支招 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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