Skip to content

Instantly share code, notes, and snippets.

@searover
Created July 22, 2015 11:27
Show Gist options
  • Save searover/521f1c891e94873e2558 to your computer and use it in GitHub Desktop.
Save searover/521f1c891e94873e2558 to your computer and use it in GitHub Desktop.
The HTTP Get and Post request method based on Apache HttpClient
package com.anchnet.ddos.report.utils;
import com.anchnet.ddos.report.beans.MessagePack;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import java.io.IOException;
/**
* Created by searover on 7/22/15.
*/
public class NetworkHelper {
public static void doGet() throws IOException {
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod("http://www.baidu.com");
client.executeMethod(method);
System.out.println(method.getStatusLine());
System.out.println(method.getResponseBodyAsString());
method.releaseConnection();
}
public static void doPost(MessagePack pack) throws IOException {
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod("http://localhost:3000/onDDOSAlarm");
System.out.println("dopost percent : " + pack.percent);
NameValuePair[] pairs = {
new NameValuePair("dstip", pack.customerIP),
new NameValuePair("percent", pack.percent.toString()),
new NameValuePair("hourse", ""),
new NameValuePair("customer", ""),
new NameValuePair("agent", pack.agentIP),
new NameValuePair("srcip",""),
new NameValuePair("dstport", pack.customerPort.toString()),
new NameValuePair("threshold", ""),
new NameValuePair("postsource", "Spark")
};
postMethod.setRequestBody(pairs);
client.executeMethod(postMethod);
System.out.println(postMethod.getStatusLine());
System.out.println(postMethod.getResponseBodyAsString());
postMethod.releaseConnection();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment