idhttp post请求返回为空

LinY_X 2018-01-16 11:37:16
这是部分Delphi代码 实际参数和地址不是问题所在,我做了隐藏。
主要功能是post请求传入用户名、密码以及一些验证参数,返回是否验证通过

procedure TForm1.Button1Click(Sender: TObject);
var
ASource: TStringList;
ResponseStr: string;
ResponseStream: TStringStream;
IdHttp: TIdHTTP;
begin
with IdHttp do
try
try
IdHttp := TIdHTTP.Create(nil);
ConnectTimeout := 2000;
ReadTimeout := 2000;
ASource := TStringList.Create;
ResponseStream := TStringStream.Create;
ASource.Add('username=admin');
ASource.Add('password=admin');
ASource.Add('type=1');
// Request.Accept := 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2';
// Request.UserAgent := 'Java/1.8.0_31';
Request.UserAgent := 'compatible; MSIE 9.0; Windows NT 6.0; .NET CLR 4.0.30319';
Request.Connection := 'keep-alive';
// Request.ContentType := '';
// ProtocolVersion := pv1_1;
// Request.AcceptEncoding := 'identity';
// Request.AcceptEncoding := '';
// Request.ContentType := 'text/html';
Post('http://test.test.com:8080/loginservice/login', ASource, ResponseStream);
// Post('http://192.168.108.18:8080/test/test.html', ASource, ResponseStream);
ResponseStr := ResponseStream.DataString;
ResponseStr := UTF8Decode(ResponseStr);
ShowMessage(ResponseStr);
except
on E: Exception do
begin
ShowMessage(E.ClassName + '///' + E.Message);
end;
end;
finally
IdHttp.Free;
ASource.Free;
ResponseStream.Free;
end;
end;

返回的ResponseStr是个空字符串
同样的接口调用 我在Java里面就能有返回值,会返回一个验证通过的字符串

public static String test(String uid, String pwd) {
HttpURLConnection httpconn = null;
String returnStr = "";
try {
URL myurl = new URL("'http://test.test.com:8080/loginservice/login'");
URLConnection conn = myurl.openConnection();
httpconn = (HttpURLConnection) conn;
httpconn.setRequestMethod("POST");
httpconn.addRequestProperty("username", uid);
httpconn.addRequestProperty("password", pwd);
httpconn.setDoOutput(true);
httpconn.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(httpconn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
returnStr += inputLine;
}
if (returnStr != null) {
returnStr = URLEncoder.encode(returnStr, "iso-8859-1");
}
in.close();

} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpconn != null) {
httpconn.disconnect();
}
}
return returnStr;
}

这个就会有返回值
所以我觉着很奇怪
...全文
1635 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
LinY_X 2018-01-16
  • 打赏
  • 举报
回复
Delphi 版本是10.1Berlin Delphi中注释的那部分是我都试过的,还是不行
Mr Dang 2018-01-16
  • 打赏
  • 举报
回复
var ASource: TStringStream; AXML是一个XML格式字符串 ,通过抓包可以看到 ASource:=TStringStream.Create(AXML,TEncoding.UTF8); ResponseStr:=Post('http://test.test.com:8080/loginservice/login', ASource);

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧