Popular Posts
Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i... LogonUser Function : impersonate a windows user // This sample demonstrates the use of the WindowsIdentity class to impersonate a user. // IMPORTANT NOTES:  // This sample can be run only ...
Stats
HttpClinet 4.01
New version means new api~
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class NetClient {

    private HttpClient client;
    private String encoding = "UTF-8";

    public NetClient() {
        this.client = new DefaultHttpClient();
    }

    private String getEncoding(Header header) {
        if (header == null)
            return null;

        Pattern p = Pattern.compile("charset=(\\S+)");
        Matcher m = p.matcher(header.getValue());
        if (m.find()) {
            return m.group(1);
        }
        return null;
    }

    private String request(HttpRequestBase request) throws ClientProtocolException, IOException {
        StringBuilder sb = new StringBuilder();
        HttpResponse response = this.client.execute(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            Header[] hds = response.getHeaders("Content-Type");
            Header contentType = hds.length > 0 ? hds[0] : null;
            String enc = this.getEncoding(contentType);

            InputStream is = entity.getContent();
            InputStreamReader isr = new InputStreamReader(is, enc == null ? this.encoding : enc);
            BufferedReader reader = new BufferedReader(isr);

            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append(System.getProperty("line.separator"));
            }

            reader.close();
            isr.close();
            is.close();
        }
        return sb.toString();
    }

    public String get(String url) throws ClientProtocolException, IOException {
        HttpGet request = new HttpGet(url);
        return this.request(request);
    }

    public String post(String url, List<NameValuePair> queryParams) throws ClientProtocolException, IOException {
        HttpPost request = new HttpPost(url);
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(queryParams, "UTF-8");
        request.setEntity(entity);
        return this.request(request);
    }

    public void setEncoding(String enc) {
        this.encoding = enc;
    }

    public String getEncoding() {
        return this.encoding;
    }

    public static void main(String[] args) throws ClientProtocolException, IOException {
        // TODO Auto-generated method stub

        NetClient nc = new NetClient();

        // String source = nc.get("http://localhost:8080/WebApp/PrintAll?a=bbb");

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("page", "22"));
        params.add(new BasicNameValuePair("action", "query"));

        String source = nc.post("http://localhost:8080/WebApp/PrintAll", params);

        System.out.println(source);
    }

}
Library : http://hc.apache.org/httpcomponents-client-4.0.1/index.html