Как отправить POST запрос не из браузера и получить ответ 200?
2 ответа
Судя по метке "java" запрос надо послать из java?
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.9</version>
</dependency>
Main
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import org.json.JSONObject;
public class App {
public static void main(String[] args) throws Exception {
JSONObject requestBody = new JSONObject()
.append("key", "value");
HttpResponse<String> stringHttpResponse = Unirest.post("https://example.com")
.body(requestBody)
.asString();
if (stringHttpResponse.getStatus() == 200) {
// тут код
}
}
}
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://www.a-domain.com/foo/");
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("param-1", "12345"));
params.add(new BasicNameValuePair("param-2", "Hello!"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
try {
// do something useful
} finally {
instream.close();
}
}
Взято отсюда.