http post


package test.net;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpApp {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HttpApp app = new HttpApp();
		app.doDownLoad("http://zhangmenshiting.baidu.com/data2/music/10509274/105092681351623661.mp3?xcode=ff8333b4fe8754a46497cf4a50e9a463");

	}

	private void userProxy(HttpClient client) {
		client.getHostConfiguration().setProxy(".com",
				8080);
		// UsernamePasswordCredentials creds = new
		// UsernamePasswordCredentials("", "");
		// client.getState().setProxyCredentials(AuthPolicy.NTLM,"", creds);
	}

	private void doDownLoad(String url) {

		HttpClient client = new HttpClient();
		userProxy(client);
		HttpMethod method = new GetMethod();

		try {
			method.setURI(new URI(url, true));
			HttpMethodParams pars = new HttpMethodParams();
			method.setParams(pars);
			int status = client.executeMethod(method);
			System.out.println("status=" + status);
			if (HttpStatus.SC_OK == status) {
				Header[] heads = method.getResponseHeaders();
				for (Header header : heads) {
					System.out.println(header.getName() + ":"
							+ header.getValue());
				}
				System.out.println(method.getResponseBodyAsString());

			}

		} catch (Exception e) {

			e.printStackTrace();
		}

	}

}



你可能感兴趣的:(http)