htttp请求,java实例

版权声明:转载请注明出处 https://blog.csdn.net/hezemin0315/article/details/53035731
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import com.fangwifi.domain.arinvoice.ArInvoice;
//import net.sf.json.JSONObject;
import com.fangwifi.domain.customer.CustomerQuery;

public class HttpClientUtil {
	static DefaultHttpClient httpClient = new DefaultHttpClient();

	public static String get(String url, String encoding) throws Exception {
		HttpGet httpGet = new HttpGet(url);
		HttpResponse res = httpClient.execute(httpGet);
		return getContent(res, encoding);
	}

	public static String get(String url, String encoding,
			DefaultHttpClient client) throws Exception {
		HttpGet httpGet = new HttpGet(url);
		HttpResponse res = client.execute(httpGet);
		return getContent(res, encoding);
	}

	public static String post(String url, StringEntity se, String host,
			String referer, String encoding) throws Exception {
		HttpPost httpPost = new HttpPost(url);
		httpPost.setEntity(se);
		httpPost.setHeader("Host", host);
		httpPost.setHeader("Referer", referer);
		httpPost.setHeader("Accept", "*/*");
		httpPost.setHeader("Accept-Language", "zh-cn");
		httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
		httpPost.setHeader("UA-CPU", "x86");
		httpPost.setHeader(
				"User-Agent",
				"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; InfoPath.2; CIBA)");
		httpPost.setHeader("Connection", "close");
		HttpResponse response = httpClient.execute(httpPost);

		return getContent(response, encoding);
	}

	public static String httpPost(String url, String queryString,
			String encoding) throws Exception {
		HttpPost httpPost = new HttpPost(url);
		httpPost.setEntity(new StringEntity(queryString,"UTF-8"));
		httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
		httpPost.getParams().setParameter("http.socket.timeout",
				new Integer(60000));
		httpPost.setHeader("Connection", "close");
		HttpResponse response = httpClient.execute(httpPost);

		return getContent(response, encoding);
	}

	public static String getContent(HttpResponse res, String encoding)
			throws Exception {
		HttpEntity ent = res.getEntity();
		String result = IOUtils.toString(ent.getContent(), encoding);
		ent.consumeContent();
		return result;
	}

	public static InputStream getStream(String url) throws Exception {
		HttpGet httpGet = new HttpGet(url);
		HttpResponse res = httpClient.execute(httpGet);
		return res.getEntity().getContent();
	}

	public static InputStream getStream(String url, DefaultHttpClient client)
			throws Exception {
		HttpGet httpGet = new HttpGet(url);
		httpGet.setHeader(
				"User-Agent",
				"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; InfoPath.2; CIBA)");
		httpGet.setHeader("Referer",
				"http://reg.126.com/regmail126/userRegist.do?action=fillinfo");
		// httpGet.setHeader("Accept", "*/*");
		// httpGet.setHeader("Accept-Language", "zh-cn");
		// httpGet.setHeader("Accept-Encoding", "gzip, deflate");
		httpGet.setHeader("Connection", "close");
		HttpResponse res = client.execute(httpGet);
		return res.getEntity().getContent();
	}

	public static void main(String args[]) throws Exception {
		//访问地址
		String url = "http://192.168.1.168:1234/customer/queryListByCode";
		
		
		CustomerQuery query = new CustomerQuery();
		query.setChannelsCode("xxxxxx");
		//发送
		String resultString = HttpClientUtil.httpPost(url, query.toJson(), "utf-8");
		System.out.println(resultString);
	}
}

注: 已测,可直接用







猜你喜欢

转载自blog.csdn.net/hezemin0315/article/details/53035731