Jump to content
Sign in to follow this  
KodeZwerg

can you help me port c# code to winapi/wininet please?

Recommended Posts

good day,

is someone willing to help me port those two c# snippets into working delphi (winapi/wininet) snippets please?


the first one:

var client = new HttpClient();
var request = new HttpRequestMessage
{
    Method = HttpMethod.Post,
    RequestUri = new Uri("https://yandextranslatezakutynskyv1.p.rapidapi.com/translate"),
    Headers =
    {
        { "x-rapidapi-key", "1234567890" },
        { "x-rapidapi-host", "YandexTranslatezakutynskyV1.p.rapidapi.com" },
    },
    Content = new FormUrlEncodedContent(new Dictionary<string, string>
    {
        { "apiKey", "1234567890" },
        { "lang", "de-en" },
        { "text", "dieser text soll übersetzt werden" },
    }),
};
using (var response = await client.SendAsync(request))
{
    response.EnsureSuccessStatusCode();
    var body = await response.Content.ReadAsStringAsync();
    Console.WriteLine(body);
}

and the second one:

var client = new HttpClient();
var request = new HttpRequestMessage
{
    Method = HttpMethod.Get,
    RequestUri = new Uri("https://systran-systran-platform-for-language-processing-v1.p.rapidapi.com/translation/text/translate?source=auto&target=en&input=dieser%20text%20soll%20%C3%BCbersetzt%20werden"),
    Headers =
    {
        { "x-rapidapi-key", "1234567890" },
        { "x-rapidapi-host", "systran-systran-platform-for-language-processing-v1.p.rapidapi.com" },
    },
};
using (var response = await client.SendAsync(request))
{
    response.EnsureSuccessStatusCode();
    var body = await response.Content.ReadAsStringAsync();
    Console.WriteLine(body);
}

thank you in advance!

Share this post


Link to post
function Http_Get(const Server: string; const Resource: string; const key: string): string;
const
  headerkey = 'x-rapidapi-key: ';
  headerhost = 'x-rapidapi-host: systran-systran-platform-for-language-processing-v1.p.rapidapi.com';
var
  hInet: HINTERNET;
  hURL: HINTERNET;
  Buffer: array[0..1023] of AnsiChar;
  i, BufferLen: cardinal;
begin
  result := '';
  hInet := InternetOpen('', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  try
    hURL := InternetOpenUrl(hInet, PChar('https://' + Server + Resource), {how to put header in here?}nil, 0, INTERNET_FLAG_SECURE , 0);
    try
      repeat
        InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
        if BufferLen = SizeOf(Buffer) then
          result := result + AnsiString(Buffer)
        else if BufferLen > 0 then
          for i := 0 to BufferLen - 1 do
            result := result + Buffer[i];
      until BufferLen = 0;
    finally
      InternetCloseHandle(hURL);
    end;
  finally
    InternetCloseHandle(hInet);
  end;
end;

i am somehow stuck... please need help

 

Share this post


Link to post
function Http_Get: string;
const
  headerkey = 'x-rapidapi-key: 1234567890';
  headerhost = 'x-rapidapi-host: systran-systran-platform-for-language-processing-v1.p.rapidapi.com';
var
  hInet: HINTERNET;
  hURL: HINTERNET;
  Buffer: array[0..1023] of AnsiChar;
  i, BufferLen: cardinal;
begin
  result := '';
  hInet := InternetOpen('', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  try
    hURL := InternetOpenUrl(hInet, PChar('https://systran-systran-platform-for-language-processing-v1.p.rapidapi.com/translation/text/translate?source=auto&target=en&input=dieser%20text%20soll%20%C3%BCbersetzt%20werden'), PChar(headerkey + headerhost), 0, INTERNET_FLAG_SECURE, 0);
    try
      repeat
        InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
        if BufferLen = SizeOf(Buffer) then
          result := result + AnsiString(Buffer)
        else if BufferLen > 0 then
          for i := 0 to BufferLen - 1 do
            result := result + Buffer[i];
      until BufferLen = 0;
    finally
      InternetCloseHandle(hURL);
    end;
  finally
    InternetCloseHandle(hInet);
  end;
end;

this works, i get result = {"message":"You are not subscribed to this API."}

now the code for post...

thank you @mvanrijnen but i like to try with wininet.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×