我试图访问json服务器中的idHTTP Delphi,但没有成功。我已经尝试过所有的替代方法,并且总是得到相同的错误:"HTTP / 1.1 401未经授权“。
用于测试的JSON格式:
{“http”:{“方法”:“POST”,"header":"access_token:55b3ce85b47629eeee778c0f0c9be450f1b1bc84cc377975f2d3d0d3808a4636","content":"name=TEST&email=teste@uol.com&phone=1147001211&mobilePhone=11992329909&address=Rua+Jose+Ricardo &addressNumber=55&province=Test¬ificationDisabled=True&city=Sao+Paulo&state=SP&country=Brasil&postalCode=05567210 &cpfCnpj=11111111111&personType=FISICA“}
用于测试的Url:
测试程序:
procedure TForm4.Button2Click(Sender: TObject);
var
sResponse: string;
EnvStr : TStringList;
begin
EnvStr := TStringList.Create;
EnvStr.AddStrings(Memo.Lines);
try
idHTTP.Request.ContentType := 'application/json';
idHTTP.Request.Method:='POST';
idHTTP.Request.AcceptCharSet := 'utf-8';
try
sResponse := idHTTP.Post(EditURL.Text,EnvStr);
except
on E: Exception do
ShowMessage('Error on request: '#13#10 + e.Message);
end;
finally
MemoRet.Lines.Clear;
MemoRet.Lines.add(sResponse);
end;
end;
用PHP发送的相同格式工作得很好,但是idHTTP会返回错误:“HTTP1.11401未经授权”。
PHP工作得很好
<?php
$api_url = "http://homolog.asaas.com/api/v2";
$api_key = "55b3ce85b47629eeee778c0f0c9be450f1b1bc84cc377975f2d3d0d3808a4636";
$url_cus = $api_url."/customers";
$param = array(
'name' => utf8_encode('Test'),
'email' => 'test@uol.com.br',
'phone' => '1147001211',
'mobilePhone' => '11992329909',
'address' => utf8_encode('Rua Jose Ricardo'),
'addressNumber' => '55',
'province' => 'Test',
'notificationDisabled' => 'True',
'city' => 'Sao Paulo',
'state' =>'SP',
'country' => 'Brasil',
'postalCode' => '05567210',
'cpfCnpj' => '11111111111',
'personType' => 'FISICA'
);
$req = http_build_query($param);
$ctx = stream_context_create(
array(
"http" => array(
"method" => "POST",
"header" => "access_token: $api_key",
"content" => $req
)
)
);
$res = file_get_contents($url_cus, true, $ctx);
//PHP Object
$obj = json_decode($res);
//get id of register
$id=utf8_decode("$obj->id");
// return result
// return $id;
?>
发布于 2016-01-28 20:03:27
我试图访问json服务器中的idHTTP Delphi,但没有成功。
您没有正确地发布JSON数据。您不能使用TStringList
,因为该版本的TIdHTTP.Post()
是用来发布HTML的,而不是发布。您需要使用TStream
来发布JSON数据,例如:
procedure TForm4.Button2Click(Sender: TObject);
var
sResponse: string;
EnvStr : TStringStream;
begin
EnvStr := TStringStream.Create(Memo.Text, TEncoding.UTF8);
try
idHTTP.Request.ContentType := 'application/json';
try
sResponse := idHTTP.Post(EditURL.Text, EnvStr);
except
on E: Exception do
ShowMessage('Error on request: '#13#10 + e.Message);
end;
finally
EnvStr.Free;
MemoRet.Text := sResponse;
end;
我已经尝试过所有的替代方法,并且总是得到相同的错误:"HTTP / 1.1 401未经授权“。
通常这意味着服务器正在请求身份验证凭据,而您并没有提供这些凭据。但是,在这种情况下,服务器的响应中没有WWW-Authenticate
头来提供挑战信息,这显然违反了HTTP协议规范。
用PHP发送的相同格式工作得很好
然后,您需要使用包嗅探器(如Wireshark )来捕获由PHP和TIdHTTP
生成的HTTP请求,然后比较它们之间的任何差异,然后根据需要将它们编码到TIdHTTP
中。
更新:基于您的PHP代码,我现在可以看到您的POST
代码正在尝试POST
一个JSON格式的字符串,但是您的POST
正在使用包含application/x-www-form-urlencoded
格式的name=value
对的HTML。请求中根本不涉及JSON。只有响应使用JSON。
现在回顾一下,PHP代码只是作用于数组,而不是真正的JSON。我认为您在这两者之间搞混了,因为数组数据的表示看起来像JSON,但实际上并非如此。如果您阅读了PHP文档,http_build_query()
只返回一个表示HTTP查询字符串的字符串,然后stream_context_create()
将创建一个基于HTTP上下文选项数组的流,其中查询字符串被设置为content
选项,然后file_get_contents()
将发送一个基于这些选项的请求--在本例中,是一个具有access_token
头和查询字符串作为消息主体的HTTP POST
请求。由于未指定Content-Type
标头,因此默认为application/x-www-form-urlencoded
。
要使用POST
使用TIdHTTP
进行application/x-www-form-urlencoded
请求,您实际上使用了带有TIdHTTP.Post()
的TStringList
,但使用了错误的数据填充了TStringList
,并且没有发送包含身份验证凭据的access_token
头。
下面的Delphi代码在我测试它时起作用:
procedure TForm4.Button2Click(Sender: TObject);
var
sResponse: string;
EnvStr : TStringList;
begin
EnvStr := TStringList.Create;
try
EnvStr.Add('name=TEST');
EnvStr.Add('email=teste@uol.com');
EnvStr.Add('phone=1147001211');
EnvStr.Add('mobilePhone=11992329909');
EnvStr.Add('address=Rua Jose Ricardo ');
EnvStr.Add('addressNumber=55');
EnvStr.Add('province=Test');
EnvStr.Add('notificationDisabled=True');
EnvStr.Add('city=Sao Paulo');
EnvStr.Add('state=SP');
EnvStr.Add('country=Brasil');
EnvStr.Add('postalCode=05567210 ');
EnvStr.Add('cpfCnpj=11111111111');
EnvStr.Add('personType=FISICA');
Http.Request.CustomHeaders.Values['access_token'] := '55b3ce85b47629eeee778c0f0c9be450f1b1bc84cc377975f2d3d0d3808a4636';
try
sResponse := idHTTP.Post(EditURL.Text, EnvStr);
except
on E: Exception do
ShowMessage('Error on request: '#13#10 + e.Message);
end;
finally
EnvStr.Free;
MemoRet.Text := sResponse;
end;
end;
收到的答复:
{“对象”:“客户”,"id":"cus_B5HmHFQSMZKD",“名称”:“测试”,“电子邮件”:“teste@uol.com”,“公司”:空,“电话”:“1147001211”,"mobilePhone":"11992329909",“地址”:“Rua Jose Ricardo","addressNumber":"55",”补语“:null,”省“:”测试“,"postalCode":"05567210","cpfCnpj":"11111111111",”personType“:”personType““已删除”:false,"notificationDisabled":true,"city":null,"state":"null","country":"Brasil","foreignCustomer":false,“foreignCustomer”:false,{“object”:“list”,"hasMore":false,“false”:100,“偏移”:0,"data":[]},"payments":{"object":"list","hasMore":false,“限制”:100,“偏移”:0,“数据”:[]},“通知”:{“对象”:“列表”,"hasMore":false,“smsEnabledForProvider”:100,“偏移”:0,“数据”:{“object”:“notification”,"id":"not_oZV4SlDvdjHf","customer":"cus_B5HmHFQSMZKD","enabled":true,"emailEnabledForProvider":true,"smsEnabledForProvider":false,"emailEnabledForCustomer":true,"smsEnabledForCustomer":true,“smsEnabledForProvider”:“PAYMENT_RECEIVED”,"scheduleOffset":0,“已删除”:false},{“object”:“not_xNHXDZb4QHqP”,“not_xNHXDZb4QHqP”,"customer":"cus_B5HmHFQSMZKD","enabled":true,"emailEnabledForProvider":true,"smsEnabledForProvider":false,"emailEnabledForCustomer":true,"smsEnabledForCustomer":true,"event":"PAYMENT_OVERDUE","scheduleOffset":0,“删除”:false},{“cus_B5HmHFQSMZKD”:“通知”,"id":"not_yt4BTyQsaRM1",“客户”:“cus_B5HmHFQSMZKD”,“已启用”:true,"emailEnabledForProvider":false,"smsEnabledForProvider":false,"emailEnabledForCustomer":true,"smsEnabledForCustomer":true,“smsEnabledForCustomer”:true:“PAYMENT_DUEDATE_WARNING”,"scheduleOffset":10,“已删除”:false},{"object":"notification","id":"not_LX1vanmAsBy9","customer":"cus_B5HmHFQSMZKD","enabled":true,"emailEnabledForProvider":false,"smsEnabledForProvider":false,"emailEnabledForCustomer":true,"smsEnabledForCustomer":true,“PAYMENT_DUEDATE_WARNING”,"scheduleOffset":0,“已删除”:false},{“object”:“通知”,"id":"not_AyYUHDExa5Zk","customer":"cus_B5HmHFQSMZKD","enabled":true,"emailEnabledForProvider":false,"smsEnabledForProvider":false,"emailEnabledForCustomer":true,"smsEnabledForCustomer":true,"event":"PAYMENT_CREATED",“scheduleOffset:0”,“删除”:false},{“对象”:“通知”,"id":"not_b6NUt9qYZrM2",“客户”:“cus_B5HmHFQSMZKD”,"enabled":true,"emailEnabledForProvider":false,"smsEnabledForProvider":false,"emailEnabledForCustomer":true,"smsEnabledForCustomer":true,“PAYMENT_UPDATED”,"scheduleOffset":0,“删除”:false},{"object":"notification","id":"not_Z4e4SHdXsJaA",“客户”:“cus_B5HmHFQSMZKD”,“启用”:true,"emailEnabledForProvider":false,"smsEnabledForProvider":false,"emailEnabledForCustomer":true,"smsEnabledForCustomer":true,“smsEnabledForCustomer”:“SEND_LINHA_DIGITAVEL”,"scheduleOffset":0,“删除”:false}}
https://stackoverflow.com/questions/35065899
复制相似问题