跳至内容

WebRequest

WebRequest

此函数向指定服务器发送HTTP请求。该函数有两个版本:

  1. 使用Content-Type: application/x-www-form-urlencoded头信息发送“key=value”类型的简单请求。
int  WebRequest(
   const string      method,           // HTTP method
   const string      url,              // URL
   const string      cookie,           // cookie
   const string      referer,          // referer
   int               timeout,          // timeout
   const char        &data[],          // the array of the HTTP message body
   int               data_size,        // data[] array size in bytes
   char              &result[],        // an array containing server response data
   string            &result_headers   // headers of server response
   );
  1. 发送任何类型的请求,并指定自定义头信息,以便更灵活地与各种Web服务进行交互。
int  WebRequest(
   const string      method,           // HTTP method
   const string      url,              // URL
   const string      headers,          // headers
   int               timeout,          // timeout
   const char        &data[],          // the array of the HTTP message body
   char              &result[],        // an array containing server response data
   string            &result_headers   // headers of server response
   );

参数

method

[in] HTTP方法。

url

[in] URL。

headers

[in] “key: value”类型的请求头信息,用换行符“\r\n”分隔。

cookie

[in] Cookie值。

referer

[in] HTTP请求中的Referer头信息的值。

timeout

[in] 超时时间(以毫秒为单位)。

data[]

[in] HTTP消息体的数据数组。

data_size

[in] data[]数组的大小。

result[]

[out] 包含服务器响应数据的数组。

result_headers

[out] 服务器响应头信息。

返回值

HTTP服务器响应代码,或错误时返回-1。

注意

要使用WebRequest()函数,请在“选项”窗口的“专家顾问”选项卡中添加所需服务器的地址到允许的URL列表中。服务器端口会根据指定的协议自动选择——“http://”为80,“https://”为443。

WebRequest()函数是同步的,这意味着它会中断程序执行并等待来自请求服务器的响应。由于接收响应的延迟可能很大,因此该函数不适用于来自指示器的调用,因为指示器在由所有指示器和图表共享的同一线程中运行。某个图表上的指示器性能延迟可能会停止更新同一符号的所有图表。

该函数只能由专家顾问和脚本调用,因为它们在自己的执行线程中运行。如果您尝试从指示器调用该函数,GetLastError()将返回错误4060——“该函数不允许调用”。

WebRequest()不能在策略测试器中执行。

使用WebRequest()函数第一版本的一个示例:

void OnStart()
  {
   string cookie=NULL,headers;
   char post[],result[];
   int res;
//--- to enable access to the server, you should add URL "https://www.google.com/finance"
//--- in the list of allowed URLs (Main Menu->Tools->Options, tab "Expert Advisors"):
   string google_url="https://www.google.com/finance";
//--- Reset the last error code
   ResetLastError();
//--- Loading a html page from Google Finance
   int timeout=5000; //--- Timeout below 1000 (1 sec.) is not enough for slow Internet connection
   res=WebRequest("GET",google_url,cookie,NULL,timeout,post,0,result,headers);
//--- Checking errors
   if(res==-1)
     {
      Print("Error in WebRequest. Error code  =",GetLastError());
      //--- Perhaps the URL is not listed, display a message about the necessity to add the address
      MessageBox("Add the address '"+google_url+"' in the list of allowed URLs on tab 'Expert Advisors'","Error",MB_ICONINFORMATION);
     }
   else
     {
      //--- Load successfully
      PrintFormat("The file has been successfully loaded, File size =%d bytes.",ArraySize(result));
      //--- Save the data to a file
      int filehandle=FileOpen("GoogleFinance.htm",FILE_WRITE|FILE_BIN);
      //--- Checking errors
      if(filehandle!=INVALID_HANDLE)
        {
         //--- Save the contents of the result[] array to a file
         FileWriteArray(filehandle,result,0,ArraySize(result));
         //--- Close the file
         FileClose(filehandle);
        }
      else Print("Error in FileOpen. Error code=",GetLastError());
     }
  }
最后更新于