我如何在java中执行这样的Post请求?
我有一个像这样的Curl的Post-request程序。我怎样才能在java中完成这项工作我如何在java中执行这样的Post请求?,java,curl,Java,Curl,我有一个像这样的Curl的Post-request程序。我怎样才能在java中完成这项工作 curl -X POST \ -H "api_key: xxxxxxxxxxxxx" \ -H "speed: 0" \ -H "voice: male" \ -H "prosody: 1" \ -H "Cache-Control: no-cache" \ -d 'This is the text to transfer' \ "http://somewhere.com" 我使用创建一个可运行的POST
curl -X POST \
-H "api_key: xxxxxxxxxxxxx" \
-H "speed: 0" \
-H "voice: male" \
-H "prosody: 1" \
-H "Cache-Control: no-cache" \
-d 'This is the text to transfer' \
"http://somewhere.com"
我使用创建一个可运行的POST
request示例
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
...
public static void main(String[] args) throws IOException {
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("https://reqres.in/api/register");
// Simple data
httppost.setEntity(
new StringEntity("{\"email\":\"eve.holt@reqres.in\",\"password\":\"pistol\"}",ContentType.create("application/json", StandardCharsets.UTF_8)));
// Simple Header
httppost.setHeader("cache-control", "no-cache");
// Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.println("Status: " + response.getStatusLine().toString());
if (entity != null) {
try (InputStream instream = entity.getContent()) {
System.out.println("response: " + IOUtils.toString(instream, StandardCharsets.UTF_8));
}
}
}
来自System.out.println()的响应
状态:HTTP/1.1200正常
响应:{“id”:4,“令牌”:“QpwL5tke4Pnpja7X4”}
我使用创建一个可运行的POST
request示例
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
...
public static void main(String[] args) throws IOException {
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("https://reqres.in/api/register");
// Simple data
httppost.setEntity(
new StringEntity("{\"email\":\"eve.holt@reqres.in\",\"password\":\"pistol\"}",ContentType.create("application/json", StandardCharsets.UTF_8)));
// Simple Header
httppost.setHeader("cache-control", "no-cache");
// Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.println("Status: " + response.getStatusLine().toString());
if (entity != null) {
try (InputStream instream = entity.getContent()) {
System.out.println("response: " + IOUtils.toString(instream, StandardCharsets.UTF_8));
}
}
}
来自System.out.println()的响应
状态:HTTP/1.1200正常
响应:{“id”:4,“令牌”:“QpwL5tke4Pnpja7X4”}
在java中转换代码
希望能有帮助
在java中转换代码
希望能有所帮助。到目前为止您尝试了什么?为什么这个标签是PHP?到目前为止您尝试了什么?为什么这个标签是PHP?非常感谢。我将此代码应用于Android应用程序,但在Android studio中,它表示HttpClient和DefaultHttpClient已被弃用。你知道我如何在Android studio中使用这些功能吗?我不熟悉Android studio和java。一次收获。谢谢,我不会使用android提供的httpclient,添加依赖项{compile“cz.msebera.android:httpclient:4.4.1.2“}。并将导入更改为cz.msebera.android.httpclient。多谢各位。我将此代码应用于Android应用程序,但在Android studio中,它表示HttpClient和DefaultHttpClient已被弃用。你知道我如何在Android studio中使用这些功能吗?我不熟悉Android studio和java。一次收获。谢谢,我不会使用android提供的httpclient,添加依赖项{compile“cz.msebera.android:httpclient:4.4.1.2“}。并将导入更改为cz.msebera.android.httpclient。更多关于