import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class Test {
public static void main(String[] args) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.157.183:8089");
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("xmldate", "<html>你好啊啊</html>"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "GBK");
entity.setContentType("text/xml; charset=utf8");
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
InputStreamReader reader = new InputStreamReader(resEntity.getContent(), "ISO-8859-1");
char[] buff = new char[1024];
int length = 0;
while ((length = reader.read(buff)) != -1) {
System.out.println(new String(buff, 0, length));
httpclient.getConnectionManager().shutdown();
}
}
}
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class Test2 {
public static void main(String[] args) throws Exception {
Test2 test=new Test2();
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
test.httpPost("http://192.168.157.183:8089",formparams);
}
public String httpPost(String url, List<NameValuePair> nvps) throws Exception {
String result = "";
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
CloseableHttpResponse httpResp = httpclient.execute(post);
try {
if (httpResp.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResp.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
// 保证连接能释放回管理器
EntityUtils.consume(entity);
}
}
} finally {
httpResp.close();
httpclient.close();
}
return result;
}
}
分享到:
相关推荐
Java模拟post请求传上传文件(文件流做参)、下载文件、好使的。
Java 模拟Ajax POST GET 提交代码,实测很好用。
java模拟客户端向服务器发送post请求
使用java处理文件上传的底层实现流程以及java模拟post协议实现文件上传
JAVA源码 模拟POST 实现程序自动登录自动注册
java语言后台请求网站操作 java中main方法发送httpPost请求
(完整版)JAVA利用HttpClient进行POST请求(HTTPS).doc
Java模拟表单post提交 HttpClient,Java开发者常用利器
运行环境jdk1.6,使用时,根据实际情况修改: map.put("userId", "320"); String temp = HttpTest.doPost("http://www.test.com/submit.jsp", map, "GBK");
java后台实现 get post 提交.zip
主要介绍了C#实现的三种模拟自动登录和提交POST信息的方法,分别列举了WebBrowser、WebClient及HttpWebRequest实现自动登录及提交POST的相关实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
Java模拟get/post登陆(含验证码),使用Httpclient模式导入eclipse即可测试
java实现调用httpclient接口的类和方法,包括了get和post传参方式,简单易懂
JAVA模拟网页发送请求,网页内容获取 ,post方式的模拟 ,利用的jcookie包(http://jcookie.sourceforge.Net/ ) 获取cookie:
主要介绍了java模拟post请求登录猫扑的小示例,需要的朋友可以参考下
使用 tess4j 识别验证码,实现java post 登录网站,忘记了很久以前写的了,改了下积分大家可以免费下载
java http-post form表单客户端请求代码,拿过去直接应用即可
JAVA-用HttpClient来模拟浏览器GET,POST.docx
C#三种模拟自动登录和提交POST信息的实现方法