extern "C" __declspec(dllexport) char * __stdcall test(char *url) //匯出函式
{
TIdSSLIOHandlerSocketOpenSSL *IdSSLIOHandlerSocketOpenSSL1=new TIdSSLIOHandlerSocketOpenSSL(NULL);
TIdHTTP *http=new TIdHTTP(NULL);
http->IOHandler=IdSSLIOHandlerSocketOpenSSL1;
IdSSLIOHandlerSocketOpenSSL1->SSLOptions->Method= sslvSSLv2;
IdSSLIOHandlerSocketOpenSSL1->SSLOptions->Mode=sslmBoth;
IdSSLIOHandlerSocketOpenSSL1->ReadTimeout=10000;
http->ReadTimeout=10000;
AnsiString html;
try {
html=http->Get(url); // 執行到這里的時候,如果回傳的資料不是很多的話,沒有問題。如果回傳的資料很多,會崩潰,貌似連catch都捕獲不了,然后跳出來個記憶體錯誤,我是用xe7寫的DLL。BCB6呼叫。
return html.c_str();
} catch ( Exception &e) {
return html.c_str() ;
}
}
uj5u.com熱心網友回復:
AnsiString html;堆疊變數改成static或者全域變數試試。
uj5u.com熱心網友回復:
可能是Get(url);方法處理不了太多的資料,一般常用TIdCustomHTTP.Get (string, TIdStream) ;如:
TStringStream *StringStream1=new TStringStream("",TEncoding::Default,true);
IdHTTP1->Get(url, StringStream1);
StringStream1->Position = 0;
StringStream1->Position = 0;
AnsiString html = StringStream1->DataString.Trim(); //DataString就是網頁內容
不過,上面的代碼有幾個問題沒解決,如網頁是utf8就會是亂碼,網路訪問出錯時,也沒有處理。我自己有個專用函式,就貼出來好了。
//---------------------------------------------------------------------------
UnicodeString __fastcall TForm1::GetUrlText(UnicodeString strUrl)
{
TIdSSLIOHandlerSocketOpenSSL *IdSSLIOHandlerSocketOpenSSL1=new TIdSSLIOHandlerSocketOpenSSL(this);
IdSSLIOHandlerSocketOpenSSL1->SSLOptions->Method=sslvSSLv3;
IdSSLIOHandlerSocketOpenSSL1->SSLOptions->Mode=sslmClient;
//可執行程式下要放ssleay32.dll和libeay32.dll兩個動態庫,否則打開https協議的網頁會報錯。
//OpenSSL Indy版下載 http://indy.fulgan.com/SSL/
//最新的32位 openssl-1.0.2o-i386-win32.zip
//最新的64位 openssl-1.0.2o-x64_86-win64.zip
TIdHTTP *IdHTTP2 = new TIdHTTP(this);
IdHTTP2->ConnectTimeout = 6000;
IdHTTP2->ReadTimeout = 6000;
IdHTTP2->Request->Accept="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
IdHTTP2->Request->UserAgent="Mozilla/5.0 (Windows NT 6.0; rv:5.0) Gecko/20100101 Firefox/5.0";
IdHTTP2->Request->AcceptCharSet="GB2312,utf-8;q=0.7,*;q=0.7";
IdHTTP2->Request->AcceptLanguage="zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3";
IdHTTP2->Request->Connection="keep-alive";
IdHTTP2->Request->ContentType="application/x-www-form-urlencoded";
IdHTTP2->ProtocolVersion=pv1_1;
IdHTTP2->ConnectTimeout = 6000;
IdHTTP2->ReadTimeout = 6000;
IdHTTP2->IOHandler=IdSSLIOHandlerSocketOpenSSL1;
//IdSSLIOHandlerSocketOpenSSL1,TIdHTTP似乎看需要會內部生成,不new和設定也能用。
TMemoryStream *StreamMSG=new TMemoryStream();
UnicodeString responsestr;
try
{
IdHTTP2->Get(strUrl.Trim(),StreamMSG);
StreamMSG->Position = 0;
if(IdHTTP2->ResponseCode==200)
{
StreamMSG->Position = 0;
((char*)StreamMSG->Memory)[(int)StreamMSG->Size]=0;
responsestr=UnicodeString((char*)StreamMSG->Memory);
if(IdHTTP2->Response->CharSet.LowerCase()=="utf-8")
{
_di_IIdTextEncoding TextEncoding1=CharsetToEncoding("utf-8");
responsestr=ReadStringFromStream(StreamMSG,-1,TextEncoding1);
//或 responsestr=ReadStringFromStream(StreamMSG,-1,IndyTextEncoding(TEncoding::UTF8));
}
}
else responsestr=L"網頁未正常回應。";
}
catch (const EIdHTTPProtocolException &E)
{
// HTTP error
// E.ErrorCode contains the ResponseCode
// E.Message contains the ResponseText
// E.ErrorMessage contains the content of the error body, if any
UnicodeString xingxi;
xingxi="HTTP error:\r\n";
xingxi+="ErrorCode:";
xingxi+=E.ErrorCode;
xingxi+="\r\n";
xingxi+="Message:";
xingxi+=E.Message;
xingxi+="\r\n";
xingxi+="ErrorMessage:";
xingxi+=E.ErrorMessage;
ShowMessage(xingxi);
}
catch (const EIdSocketError &E)
{
// Socket error
// E.LastError contains the socket error code
// E.Message contains the socket error message
if (E.LastError == Id_WSAECONNREFUSED)
ShowMessage(L"Your connection is refused");
else
ShowMessage(E.Message);
}
catch (const EIdException &E)
{
// any other Indy error
ShowMessage("Error:"+E.Message);
}
catch(Exception &E)
{
// any other non-Indy error
ShowMessage("Error:"+E.Message);
}
delete StreamMSG;
delete IdHTTP2;
return responsestr;
}
//---------------------------------------------------------------------------
uj5u.com熱心網友回復:
只是在DLL里 會出現這個問題,在form application 里不會。uj5u.com熱心網友回復:
謝謝,晚會我試試。
uj5u.com熱心網友回復:
要呼叫的我的函式也很簡單,extern "C" __declspec(dllexport) char * __stdcall test(char *url) //匯出函式
{
AnsiString html;
html=GetUrlText(url);
return html.c_str() ;
}
即可。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/47620.html
標籤:茶館
下一篇:Failed to write core dump. Core dumps have been disabled
