java中如何用POST请求url呢
pom依赖
<dependency>
<groupId>org.asynchttpclient</groupId>
<artifactId>async-http-client</artifactId>
<version>2.6.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.59</version>
</dependency>
上代码
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.BoundRequestBuilder;
import org.asynchttpclient.Response;
import java.util.concurrent.Future;
import static org.asynchttpclient.Dsl.asyncHttpClient;
public class UrlUtils {
private static AsyncHttpClient asyncHttpClient = null;
static {
asyncHttpClient = asyncHttpClient();
}
public static String preparePostUrl(String url,String body){
try{
BoundRequestBuilder builder = asyncHttpClient.preparePost(url).setRequestTimeout(10000);
builder.setHeader("Content-Type","application/json; charset=utf-8");
builder.setBody(body);
builder.setMethod("POST");
Future<Response> whenResponse = builder.execute();
return whenResponse.get().getResponseBody();
}catch (Exception e){
e.fillInStackTrace();
}
return null;
}
}
测试
preparePostUrl("http","")