我正在使用HttpsURLConnection连接到应用程序。一切都很好,直到我在请求中尝试使用参数(例如用户名和密码)之前,因为在服务器端空值已映射到参数。
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.addRequestProperty("password", "xxxx");
connection.addRequestProperty("username", "aaaaaa");
connection.addRequestProperty("eventId","btnLogin");
connection.setRequestMethod("POST"); //$NON-NLS-1$
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty( "Accept", "*/*" );
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
我提到HttpsURLConnection可以工作,并且初始登录页面是通过HttpsUrlConnection检索的。
如何通过POST请求在服务器端成功发送参数。
此致
答案 0 :(得分:0)
为了解决该问题,我创建了一个对HashMap对象的Map引用,该对象在键中存储了参数,在值中存储了这些参数的值。
之后,我创建了一个方法,该方法将返回一个String对象,其中包含用于POST请求的编码参数。
class SplashBloc extends Bloc<SplashEvent, SplashState> {
final FirebaseService firebaseService;
final DevicesService devicesService;
final AuthService authService;
final UserPreferences _userPreferences = UserPreferences();
SplashBloc({
@required this.firebaseService,
@required this.devicesService,
@required this.authService,
});
@override
Stream<SplashEvent> transform(Stream<SplashEvent> events) {
return (events as Observable<SplashEvent>).debounce(
Duration(milliseconds: 500));
}
@override
get initialState => SplashInitial();
@override
Stream<SplashState> mapEventToState(currentState, event) async* {
if (event is SplashInitEvent) {
if (currentState is SplashInitial) {
yield InitLoading();
try {
firebaseService.togglePerformanceCollection(true);
firebaseService.firebaseCloudMessagingListeners();
String firebaseToken = await firebaseService
.getFirebaseMessagingToken();
bool isRegistered =
await _userPreferences.getIsDeviceRegistered() ?? false;
if (!isRegistered) {
final String platform = event.isIOS ? 'IOS' : 'Android';
final deviceInfo = await devicesService.getDeviceInfo(platform);
isRegistered = await devicesService.register(
deviceToken: firebaseToken,
deviceInfo: deviceInfo,
);
if (isRegistered) {
_userPreferences.setIsDeviceRegistered(true);
}
}
yield InitSuccess();
} catch (e) {
yield InitFailure(error: e.toString());
}
}
}
if (event is SplashInitialEvent) {
yield SplashInitial();
}
}
}
以上所有内容都在输出流中使用,以便将它们发送到服务器。
private String getOutput(Map<String, String> hm) throws UnsupportedEncodingException {
String out = "";
boolean start = true;
for(Map.Entry<String, String> h : hm.entrySet()){
if(start){
start=false;
out+=URLEncoder.encode(h.getKey(), "UTF-8");
out+="=";
out+=URLEncoder.encode(h.getValue(), "UTF-8");
}
else{
out += "&";
out+=URLEncoder.encode(h.getKey(), "UTF-8");
out+="=";
out+=URLEncoder.encode(h.getValue(), "UTF-8");
}
}