Skip to content

Instantly share code, notes, and snippets.

@searover
Created July 22, 2015 11:27
Show Gist options
  • Select an option

  • Save searover/521f1c891e94873e2558 to your computer and use it in GitHub Desktop.

Select an option

Save searover/521f1c891e94873e2558 to your computer and use it in GitHub Desktop.

Revisions

  1. searover created this gist Jul 22, 2015.
    52 changes: 52 additions & 0 deletions NetworkHelper.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    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();
    }
    }