在 URL 中发送带有查询字符串的 HTTP Post
Send HTTP Post with querystring in URL
我对使用查询字符串发送 HTTP Post 有疑问。
我有下面的代码,但是这些代码不起作用。我尝试通过网络服务发送用户和密码,嵌入在 URL 中,但它不起作用。此代码无法连接到网络服务。
@Override
protected String doInBackground(String... params) {
String result = "";
try {
URL url = new URL("http://192.168.0.11:8080/api/Usuario/doLogin?user="+user+"&senha="+password);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = bufferedReader.readLine()) != null) {
response.append(inputLine);
}
result = response.toString();
bufferedReader.close();
} catch (Exception e) {
Log.d("InputStream", e.getMessage());
}
return result;
}
我想你的意思是 GET 请求不是 POST,
并且您应该在查询参数中对变量进行编码,在您的情况下为 "user" 和 "password"。
URL url = new URL("http://192.168.0.11:8080/api/Usuario/doLogin?user=" + URLEncoder.encode(user, "UTF-8")+"&senha="+ URLEncoder.encode(password, "UTF-8"));
我对使用查询字符串发送 HTTP Post 有疑问。
我有下面的代码,但是这些代码不起作用。我尝试通过网络服务发送用户和密码,嵌入在 URL 中,但它不起作用。此代码无法连接到网络服务。
@Override
protected String doInBackground(String... params) {
String result = "";
try {
URL url = new URL("http://192.168.0.11:8080/api/Usuario/doLogin?user="+user+"&senha="+password);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = bufferedReader.readLine()) != null) {
response.append(inputLine);
}
result = response.toString();
bufferedReader.close();
} catch (Exception e) {
Log.d("InputStream", e.getMessage());
}
return result;
}
我想你的意思是 GET 请求不是 POST, 并且您应该在查询参数中对变量进行编码,在您的情况下为 "user" 和 "password"。
URL url = new URL("http://192.168.0.11:8080/api/Usuario/doLogin?user=" + URLEncoder.encode(user, "UTF-8")+"&senha="+ URLEncoder.encode(password, "UTF-8"));