import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
public class GetRequestExample {
public static void main(String[] args) {
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod("https://api.example.com/users");
try {
int statusCode = client.executeMethod(getMethod);
String responseBody = getMethod.getResponseBodyAsString();
System.out.println("Status Code: " + statusCode);
System.out.println("Response Body: " + responseBody);
} catch (Exception e) {
e.printStackTrace();
} finally {
getMethod.releaseConnection();
}
}
}
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.NameValuePair;
public class PostRequestExample {
public static void main(String[] args) {
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod("https://api.example.com/users");
NameValuePair[] data = {
new NameValuePair("username", "john"),
new NameValuePair("password", "secret")
};
postMethod.setRequestBody(data);
try {
int statusCode = client.executeMethod(postMethod);
String responseBody = postMethod.getResponseBodyAsString();
System.out.println("Status Code: " + statusCode);
System.out.println("Response Body: " + responseBody);
} catch (Exception e) {
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
}
}
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
public class SetRequestHeadersExample {
public static void main(String[] args) {
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod("https://api.example.com/users");
getMethod.addRequestHeader("User-Agent", "Mozilla/5.0");
getMethod.addRequestHeader("Accept-Language", "en-US,en;q=0.5");
getMethod.addRequestHeader("Authorization", "Bearer your_auth_token");
try {
int statusCode = client.executeMethod(getMethod);
String responseBody = getMethod.getResponseBodyAsString();
System.out.println("Status Code: " + statusCode);
System.out.println("Response Body: " + responseBody);
} catch (Exception e) {
e.printStackTrace();
} finally {
getMethod.releaseConnection();
}
}
}
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
public class SetProxyExample {
public static void main(String[] args) {
HttpClientParams params = new HttpClientParams();
params.setParameter("http.useragent", "Mozilla/5.0");
params.setParameter("http.proxyHost", "your_proxy_host");
params.setParameter("http.proxyPort", "your_proxy_port");
HttpClient client = new HttpClient(params);
GetMethod getMethod = new GetMethod("https://api.example.com/users");
try {
int statusCode = client.executeMethod(getMethod);
String responseBody = getMethod.getResponseBodyAsString();
System.out.println("Status Code: " + statusCode);
System.out.println("Response Body: " + responseBody);
} catch (Exception e) {
e.printStackTrace();
} finally {
getMethod.releaseConnection();
}
}
}