java 中的 HTTP POST 请求
HTTP POST request in java
我是 Java/Android 的初学者,我必须向 Web 服务发出 http post 请求以获取密钥(我 post 一个日期,然后我获取密钥).
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class HttpsPostRequest extends AsyncTask<Void, Void, String> {
private Context context;
private String content; //Body
private String request;
protected static ProgressDialog progressDialog = null;
public HttpsPostRequest(Context context, String request, String content) {
this.context = context;
this.request = request;
this.content = content;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(context, "Sending informations", "Please wait ...", false, false);
}
@Override
protected String doInBackground(Void... voids) {
InputStream inputStream = null;
HttpsURLConnection urlConnection = null;
StringBuffer json = null;
try {
URL url = new URL(request);
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setDoOutput(true); // indicates POST method
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
DataOutputStream dataOutputStream = new DataOutputStream(urlConnection.getOutputStream());
dataOutputStream.writeBytes(content);
dataOutputStream.flush();
dataOutputStream.close();
int reponse = urlConnection.getResponseCode();
if (reponse != 200)
return "Error";
inputStream = urlConnection.getInputStream();
if (inputStream == null)
return "Error";
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader reader = new BufferedReader(inputStreamReader);
json = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
json.append(line);
json.append("\n");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null)
urlConnection.disconnect();
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ignored) { }
}
}
if (json == null)
return "Error";
if (new String(json).contains("{\"success\":true"))
return new String(json);
return "Error";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
下一个问题是,服务器 return 此请求的 "GET" 响应,即使我说它是 "POST" 并且当我尝试使用 RESTClient 测试我的请求时它是return 好答案。所以我需要你的帮助,我是不是忘记了什么?
请删除下一行
urlConnection.setDoInput(true);
在 GET 请求中,参数作为 URL 的一部分发送。
在 POST 请求中,参数作为请求的 body 发送,在 headers.
之后
做一个POST与HttpURL的连接,需要在打开连接后将参数写入连接。
这段代码应该可以帮助您入门:
urlParameters 将是您的要求。
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
URL url = new URL( request );
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty( "charset", "utf-8");
conn.setUseCaches( false );
try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
wr.write( postData );
}
我是 Java/Android 的初学者,我必须向 Web 服务发出 http post 请求以获取密钥(我 post 一个日期,然后我获取密钥).
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class HttpsPostRequest extends AsyncTask<Void, Void, String> {
private Context context;
private String content; //Body
private String request;
protected static ProgressDialog progressDialog = null;
public HttpsPostRequest(Context context, String request, String content) {
this.context = context;
this.request = request;
this.content = content;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(context, "Sending informations", "Please wait ...", false, false);
}
@Override
protected String doInBackground(Void... voids) {
InputStream inputStream = null;
HttpsURLConnection urlConnection = null;
StringBuffer json = null;
try {
URL url = new URL(request);
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setDoOutput(true); // indicates POST method
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
DataOutputStream dataOutputStream = new DataOutputStream(urlConnection.getOutputStream());
dataOutputStream.writeBytes(content);
dataOutputStream.flush();
dataOutputStream.close();
int reponse = urlConnection.getResponseCode();
if (reponse != 200)
return "Error";
inputStream = urlConnection.getInputStream();
if (inputStream == null)
return "Error";
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader reader = new BufferedReader(inputStreamReader);
json = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
json.append(line);
json.append("\n");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null)
urlConnection.disconnect();
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ignored) { }
}
}
if (json == null)
return "Error";
if (new String(json).contains("{\"success\":true"))
return new String(json);
return "Error";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
下一个问题是,服务器 return 此请求的 "GET" 响应,即使我说它是 "POST" 并且当我尝试使用 RESTClient 测试我的请求时它是return 好答案。所以我需要你的帮助,我是不是忘记了什么?
请删除下一行
urlConnection.setDoInput(true);
在 GET 请求中,参数作为 URL 的一部分发送。
在 POST 请求中,参数作为请求的 body 发送,在 headers.
之后做一个POST与HttpURL的连接,需要在打开连接后将参数写入连接。
这段代码应该可以帮助您入门: urlParameters 将是您的要求。
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
URL url = new URL( request );
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty( "charset", "utf-8");
conn.setUseCaches( false );
try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
wr.write( postData );
}