`

HttpClient

 
阅读更多
	/**
	 * 通过get请求往服务器提交数据
	 * @param path  请求路径
	 * @param username  用户名
	 * @param password  密码
	 * @return
	 * @throws Exception
	 */
	public boolean loginByGet(String path,String username,String password) throws Exception{
		///http://192.168.1.101:8080/web/LoginServlet?name=%E7%BE%8E%E5%A5%B3&password=123456
		StringBuilder sb = new StringBuilder(path);
		sb.append("?");
		sb.append("name=").append(URLEncoder.encode(username, "utf-8"));
		sb.append("&");
		sb.append("password=").append(password);
		
		URL url = new URL(sb.toString());
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
		if(conn.getResponseCode() == 200){
			return true;
		}
		return false;
	}
	
	/**通过post请求向服务器提交数据
	 * post   请求首先是先把数据写入到缓存。一定要向服务器去获取数据
	 * @param path
	 * @param username
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public boolean loginByPost(String path,String username,String password) throws Exception{
		
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("POST");
		
		//name=%E5%8F%B0%E6%B9%BE%E5%AF%8C%E5%B0%91&password=123456
		StringBuilder sb = new StringBuilder();
		sb.append("name=").append(URLEncoder.encode(username, "utf-8")).append("&");
		sb.append("password=").append(password);
		byte[] entity = sb.toString().getBytes();
		
		//设置请求参数
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//实体参数的类型
		conn.setRequestProperty("Content-Length", entity.length+"");//实体参数的长度
		//允许对外输出
		conn.setDoOutput(true);
		OutputStream os = conn.getOutputStream();
		os.write(entity);
		
		if(conn.getResponseCode() == 200){
			return true;
		}
		return false;
	}
	
	/**
	 * 通过HttpClient  以get请求向服务器提交数据
	 * @param path
	 * @param username
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public boolean loginByHttpClientGet(String path,String username,String password) throws Exception{
		
		StringBuilder sb = new StringBuilder(path);
		sb.append("?");
		sb.append("name=").append(URLEncoder.encode(username, "utf-8"));
		sb.append("&");
		sb.append("password=").append(password);
		
		//1 得到浏览器
		HttpClient httpClient = new DefaultHttpClient();//浏览器
		
		//2 指定请求方式
		HttpGet httpGet = new HttpGet(sb.toString());
		
		//3执行请求
		HttpResponse httpResponse = httpClient.execute(httpGet);
		
		//4判断请求是否成功
		int statusCode = httpResponse.getStatusLine().getStatusCode();
		if(statusCode == 200){
			return true;
		}
		return false;
	}
	
	/**
	 * 通过httpClient  以post请求向服务器发送数据
	 * @param path
	 * @param username
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public boolean loginHttpClientByPost(String path,String username,String password) throws Exception{
		
		//1 得到浏览器
		HttpClient httpClient = new DefaultHttpClient();
		
		//2 指定请求方式
		HttpPost httpPost = new HttpPost(path);
		
		//3构建请求实体的数据
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("name", username));
		parameters.add(new BasicNameValuePair("password", password));
		
		//4 构建实体
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "utf-8");
		
		//5 把实体数据设置到请求对象
		httpPost.setEntity(entity);
		
		//6 执行请求
		HttpResponse httpResponse = httpClient.execute(httpPost);
		
		//7 判断请求是否成功
		if(httpResponse.getStatusLine().getStatusCode() == 200){
			return true;
		}
		
		return false;
	}
	

    /**
     * 通过httpClient 3.1 来实现文件的上传	
     * @param path  路径
     * @param username  用户名
     * @param password  密码
     * @param filename  文件路径名
     * @return
     * @throws Exception
     */
	public boolean uploadFile(String path,String username,String password,String filename) throws Exception{
		
		//1 得到浏览器
		org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
		
		//2确定请求方式
		PostMethod postMethod = new PostMethod(path);
		
		//3 确定请求的参数
		Part[] parts = new Part[]{new StringPart("name", username),
				new StringPart("password", password),
				new FilePart("file", new File(filename))};
		
		//4 构建实体
		MultipartRequestEntity entity = new MultipartRequestEntity(parts, postMethod.getParams());
		
		//5 设置实体
		postMethod.setRequestEntity(entity);
		
		//6 执行请求
		int responseCode = httpClient.executeMethod(postMethod);
		
		// 7判断请求是否成功
		if(responseCode == 200){
			return true;
		}
		
		return false;
	}

 

分享到:
评论

相关推荐

    httpClient

    HttpClient httpClient = new HttpClient(); // 设置 Http 连接超时为5秒 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); /* 2 生成 GetMethod 对象并设置参数 */ GetMethod ...

    httpclient-4.5.6-API文档-中文版.zip

    赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...

    httpclient

    使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。 1. 创建HttpClient对象。 2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建...

    httpclient-4.5.13-API文档-中英对照版.zip

    赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...

    httpclient-4.5.5-API文档-中文版.zip

    赠送jar包:httpclient-4.5.5.jar; 赠送原API文档:httpclient-4.5.5-javadoc.jar; 赠送源代码:httpclient-4.5.5-sources.jar; 包含翻译后的API文档:httpclient-4.5.5-javadoc-API文档-中文(简体)版.zip ...

    httpclient-4.5.13-API文档-中文版.zip

    赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...

    可用org.apache.commons.httpclient-3.1.0.jar.zip

    import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods....

    httpclient-4.5jar

    httpclient-4.5所需jar包,里面包含httpclient-4.5.jar等等10个必须的开发包。 1.commons-codec-1.9.jar 2.commons-logging-1.2.jar 3.fluent-hc-4.5.jar 4.httpclient-4.5.jar 5.httpclient-cache-4.5.jar 6....

    httpclient4.5.3 jar完整包含所有依赖包

    HttpClient 4.5.3 (GA) is a maintenance release that fixes a number of defects found since 4.5.2. Please note that as of 4.4 HttpClient requires Java 1.6 or newer. Changelog: ------------------- * ...

    httpclient.jar包下载

    httpclient.jar下载 包括code.jar包

    SpringBoot使用httpclient发送Post请求时

    try(CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(url); StringEntity stringEntity = new StringEntity(params, Charset.forName("UTF-8")); ...

    Android_HttpClient_jar包

    Android使用HttpClient发送请求、接收响应很简单,只要如下几步即可: Step1:创建HttpClient对象; Step2:如果需要发送GET请求,则创建HttpGet对象; 如果需要发送POST请求,则创建HttpPost对象; Step3:如果...

    httpclient-4.2.5-API文档-中文版.zip

    赠送jar包:httpclient-4.2.5.jar; 赠送原API文档:httpclient-4.2.5-javadoc.jar; 赠送源代码:httpclient-4.2.5-sources.jar; 赠送Maven依赖信息文件:httpclient-4.2.5.pom; 包含翻译后的API文档:httpclient...

    httpclient-4.5.10-API文档-中文版.zip

    赠送jar包:httpclient-4.5.10.jar; 赠送原API文档:httpclient-4.5.10-javadoc.jar; 赠送源代码:httpclient-4.5.10-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.10.pom; 包含翻译后的API文档:...

    HttpClient以及获取页面内容应用

    压缩包中含有多个文档,从了解httpclient到应用。 httpClient 1httpClint 1.1简介 HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持...

    httpclient-4.5.2-API文档-中文版.zip

    赠送jar包:httpclient-4.5.2.jar; 赠送原API文档:httpclient-4.5.2-javadoc.jar; 赠送源代码:httpclient-4.5.2-sources.jar; 包含翻译后的API文档:httpclient-4.5.2-javadoc-API文档-中文(简体)版.zip ...

    httpclient-4.4-API文档-中文版.zip

    赠送jar包:httpclient-4.4.jar; 赠送原API文档:httpclient-4.4-javadoc.jar; 赠送源代码:httpclient-4.4-sources.jar; 赠送Maven依赖信息文件:httpclient-4.4.pom; 包含翻译后的API文档:httpclient-4.4-...

    HttpClient 3.x to HttpComponents HttpClient 4.x

    帮助程序员快速从Apache的HttpClient 3.x升级到HttpClient 4.x

    httpclient-4.5.3-API文档-中文版.zip

    赠送jar包:httpclient-4.5.3.jar 赠送原API文档:httpclient-4.5.3-javadoc.jar 赠送源代码:httpclient-4.5.3-sources.jar 包含翻译后的API文档:httpclient-4.5.3-javadoc-API文档-中文(简体)版.zip 对应Maven...

Global site tag (gtag.js) - Google Analytics