1. 程式人生 > >java 傳送get post請求

java 傳送get post請求

1、匯入jar

<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
</dependency>

2、 傳送get請求  ,返回json字串

public static String getJsonString4Url(String url) throws IOException {
    HttpClient client=new HttpClient();
    GetMethod getMethod=new GetMethod(url);
    client.executeMethod(getMethod);
    return getMethod.getResponseBodyAsString();
}

3、傳送post請求  返回json字串

public static String getJsonString4Url(String url) throws IOException {
    HttpClient client=new HttpClient();
    PostMethod method = new PostMethod(url);
    client.executeMethod(getMethod);
    return getMethod.getResponseBodyAsString();
}