1,593
社区成员
发帖
与我相关
我的任务
分享
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;
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;
}