和標題一樣,Postman 正常匯入 JSON 檔案時,使用 Delphi Get 方法編碼加載檔案時,獲取 XML 格式。使用了之前很好用的GET方法代碼,但是不知道是什么問題。
哦,源代碼'idhttps.Request.CustomHeaders.Add('ContentType:application/json');' 被添加是因為對方是這樣設定的。:(
※非常抱歉,因為我是一個不會說英語的韓國人,所以通過翻譯問了你一個問題。
begin
idhttps := TIdHTTP.Create;
//===========================idhttp Get=========================
try
//Edit1.text := Base64Encode(Edit1.Text); //Edit1.Text?? Base64??? ??
//EditBASE64.Text := stringreplace(edit1.Text,' ','+',[rfReplaceAll]); // ??????? ?????
APIURL := 'URL'; //API URL
//======================SSL ??============================
sslIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(idhttps);
sslIOHandler.SSLOptions.Mode := sslmClient;
sslIOHandler.SSLOptions.Method := sslvSSLv23;
idhttps.IOHandler := sslIOHandler;
//======================SSL ?? ?============================
//======================idhttp================================
idHttps.Request.Clear;
idhttps.Request.Method:='GET';
idhttps.Request.ContentType := 'application/json';
idhttps.Request.CustomHeaders.Add('ContentType:application/json');
idhttps.Request.CustomHeaders.Add('keycode:ACCESSCODE');
idhttps.HandleRedirects := False;
idhttps.ConnectTimeout := 10000;
idhttps.ReadTimeout := 10000;
//======================idhttp================================
//======================UTF-8??=============================
idHttps.Request.AcceptCharSet := 'utf-8';
//======================UTF-8?? ?=============================
idhttps.HTTPOptions := idhttps.HTTPOptions [hoNoProtocolErrorException, hoWantProtocolErrorContent];
lstream := TStringStream.Create('', TEncoding.UTF8); // TStringStream.Create('', TEncoding.UTF8);
idhttps.Get(APIURL, lStream);
Result := (lStream.DataString);
finally
idhttps.Free; //???? ?????
end;
Edit4.Text := Result;
uj5u.com熱心網友回復:
您需要Accept在請求中使用標頭(不是Content-Type)并發送application/json。我也見過text/json用過的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/503909.html
