Einzelnen Beitrag anzeigen

mytbo

Registriert seit: 8. Jan 2007
461 Beiträge
 
#17

AW: Maschinesteuern mit TNetHttpClient

  Alt 13. Apr 2024, 23:03
Hat jemand da Erfahrung und eventuell einen Beispielcode?
Du kannst Curl nehmen. Wie ist in diesem Post beschrieben.
Delphi-Quellcode:
uses
  mormot.core.base,
  mormot.core.data,
  mormot.core.text,
  mormot.core.unicode,
  mormot.lib.curl;

const
  REQUEST_URL: RawUtf8 = 'http://192.168.178.25:8080/setKeys';
var
  hnd: TCurl;
  res: TCurlResult;
  statusCode: Integer;
  requestData: RawUtf8;
  responseData: RawByteString;
begin
  if not CurlIsAvailable then Exit; //=>

  hnd := curl.easy_init;
  if hnd <> Nil then
  try
    curl.easy_setopt(hnd, coWriteFunction, @CurlWriteRawByteString);
    curl.easy_setopt(hnd, coWriteData, @responseData);

    // Basic authentication
    curl.easy_setopt(hnd, coUserName, RawUtf8('USERNAME'));
    curl.easy_setopt(hnd coPassword, RawUtf8('PASSWORD'));

    // Post request data
    requestData := 'Stromf_Ew.Anforderung_GLT.bAktiv=1';
    curl.easy_setopt(hnd, coPostFields, Pointer(requestData));
    curl.easy_setopt(hnd, coPostFieldSize, Length(requestData));

    curl.easy_setopt(hnd, coURL, Pointer(REQUEST_URL));
    curl.easy_setopt(hnd, coCustomRequest, RawUtf8('POST'));
    res := curl.easy_perform(hnd);
    if res = crOk then
    begin
      curl.easy_getinfo(hnd, ciResponseCode, statusCode);
      case statusCode of
        200:
          ShowMessage(Utf8ToString(RawUtf8(responseData))); // Stromf_Ew.Anforderung_GLT.bAktiv ok
        else
          ShowMessage('Should not happen.');
      end;
    end
    else
      ShowMessage(Format('Error: %d (%s)', [Ord(res), curl.easy_strerror(res)]));
  finally
    curl.easy_cleanup(hnd);
  end;
end;
Bis bald...
Thomas
  Mit Zitat antworten Zitat