HttpClient4.2.2的几个常用方法,登录之后访问页面问题,下载文件

在工作中要用到android,然后进行网络请求的时候,打算使用httpClient。

总结一下httpClient的一些基本使用。

版本是4.2.2。

使用这个版本的过程中,百度很多,结果都是出现的org.apache.commons.httpclient.这个包名,而不是我这里的org.apache.http.client.HttpClient----------前者版本是Commons
HttpClient 3.x ,不是最新的版本HttpClient 4.×。

官网上面:

Commons HttpClient 3.x codeline is at the end of life. All users of Commons  HttpClient 3.x are strongly encouraged to upgrade to HttpClient 4.1. 

1.基本的get

Java代码

  1. public void getUrl(String url, String encoding) 
  2.             throws ClientProtocolException, IOException { 
  3.         // 默认的client类。 
  4.         HttpClient client = new DefaultHttpClient(); 
  5.         // 设置为get取连接的方式. 
  6.         HttpGet get = new HttpGet(url); 
  7.         // 得到返回的response. 
  8.         HttpResponse response = client.execute(get); 
  9.         // 得到返回的client里面的实体对象信息. 
  10.         HttpEntity entity = response.getEntity(); 
  11.         if (entity != null) { 
  12.             System.out.println("内容编码是:" + entity.getContentEncoding()); 
  13.             System.out.println("内容类型是:" + entity.getContentType()); 
  14.             // 得到返回的主体内容. 
  15.             InputStream instream = entity.getContent(); 
  16.             try { 
  17.                 BufferedReader reader = new BufferedReader( 
  18.                         new InputStreamReader(instream, encoding)); 
  19.                 System.out.println(reader.readLine()); 
  20.             } catch (Exception e) { 
  21.                 e.printStackTrace(); 
  22.             } finally { 
  23.                 instream.close(); 
  24.             } 
  25.         } 
  26.  
  27.         // 关闭连接. 
  28.         client.getConnectionManager().shutdown(); 
  29.     } 
public void getUrl(String url, String encoding)
			throws ClientProtocolException, IOException {
		// 默认的client类。
		HttpClient client = new DefaultHttpClient();
		// 设置为get取连接的方式.
		HttpGet get = new HttpGet(url);
		// 得到返回的response.
		HttpResponse response = client.execute(get);
		// 得到返回的client里面的实体对象信息.
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			System.out.println("内容编码是:" + entity.getContentEncoding());
			System.out.println("内容类型是:" + entity.getContentType());
			// 得到返回的主体内容.
			InputStream instream = entity.getContent();
			try {
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(instream, encoding));
				System.out.println(reader.readLine());
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				instream.close();
			}
		}

		// 关闭连接.
		client.getConnectionManager().shutdown();
	}

2.基本的Post

   下面的params参数,是在表单里面提交的参数。

Java代码

  1. public void postUrlWithParams(String url, Map params, String encoding) 
  2.             throws Exception { 
  3.         DefaultHttpClient httpclient = new DefaultHttpClient(); 
  4.         try { 
  5.  
  6.             HttpPost httpost = new HttpPost(url); 
  7.             // 添加参数 
  8.             List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 
  9.             if (params != null && params.keySet().size() >0) { 
  10.                 Iterator iterator = params.entrySet().iterator(); 
  11.                 while (iterator.hasNext()) { 
  12.                     Map.Entry entry = (Entry) iterator.next(); 
  13.                     nvps.add(new BasicNameValuePair((String) entry.getKey(), 
  14.                             (String) entry.getValue())); 
  15.                 } 
  16.             } 
  17.  
  18.             httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); 
  19.  
  20.             HttpResponse response = httpclient.execute(httpost); 
  21.             HttpEntity entity = response.getEntity(); 
  22.  
  23.             System.out.println("Login form get: " + response.getStatusLine() 
  24.                     + entity.getContent()); 
  25.             dump(entity, encoding); 
  26.             System.out.println("Post logon cookies:"); 
  27.             List<Cookie> cookies = httpclient.getCookieStore().getCookies(); 
  28.             if (cookies.isEmpty()) { 
  29.                 System.out.println("None"); 
  30.             } else { 
  31.                 for (int i =0; i < cookies.size(); i++) { 
  32.                     System.out.println("- " + cookies.get(i).toString()); 
  33.                 } 
  34.             } 
  35.  
  36.         } finally { 
  37.             // 关闭请求 
  38.             httpclient.getConnectionManager().shutdown(); 
  39.         } 
  40.     } 
public void postUrlWithParams(String url, Map params, String encoding)
			throws Exception {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		try {

			HttpPost httpost = new HttpPost(url);
			// 添加参数
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			if (params != null && params.keySet().size() > 0) {
				Iterator iterator = params.entrySet().iterator();
				while (iterator.hasNext()) {
					Map.Entry entry = (Entry) iterator.next();
					nvps.add(new BasicNameValuePair((String) entry.getKey(),
							(String) entry.getValue()));
				}
			}

			httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

			HttpResponse response = httpclient.execute(httpost);
			HttpEntity entity = response.getEntity();

			System.out.println("Login form get: " + response.getStatusLine()
					+ entity.getContent());
			dump(entity, encoding);
			System.out.println("Post logon cookies:");
			List<Cookie> cookies = httpclient.getCookieStore().getCookies();
			if (cookies.isEmpty()) {
				System.out.println("None");
			} else {
				for (int i = 0; i < cookies.size(); i++) {
					System.out.println("- " + cookies.get(i).toString());
				}
			}

		} finally {
			// 关闭请求
			httpclient.getConnectionManager().shutdown();
		}
	}

3。打印页面输出的小代码片段

Java代码

  1. private staticvoid dump(HttpEntity entity, String encoding) 
  2.             throws IOException { 
  3.         BufferedReader br = new BufferedReader(new InputStreamReader( 
  4.                 entity.getContent(), encoding)); 
  5.         System.out.println(br.readLine()); 
  6.     } 
private static void dump(HttpEntity entity, String encoding)
			throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(
				entity.getContent(), encoding));
		System.out.println(br.readLine());
	}

4.常见的登录session问题,需求:使用账户,密码登录系统之后,然后再访问页面不出错。

特别注意,下面的httpclient对象要使用一个,而不要在第二次访问的时候,重新new一个。至于如何保存这个第一步经过了验证的httpclient,有很多种方法实现。单例,系统全局变量(android 下面的Application),ThreadLocal变量等等。

       以及下面创建的httpClient要使用ThreadSafeClientConnManager对象!

   public String getSessionId(String url, Map params, String encoding,

Java代码

  1.         String url2) throws Exception { 
  2.     DefaultHttpClient httpclient = new DefaultHttpClient( 
  3.             new ThreadSafeClientConnManager()); 
  4.     try { 
  5.  
  6.         HttpPost httpost = new HttpPost(url); 
  7.         // 添加参数 
  8.         List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 
  9.         if (params != null && params.keySet().size() >0) { 
  10.             Iterator iterator = params.entrySet().iterator(); 
  11.             while (iterator.hasNext()) { 
  12.                 Map.Entry entry = (Entry) iterator.next(); 
  13.                 nvps.add(new BasicNameValuePair((String) entry.getKey(), 
  14.                         (String) entry.getValue())); 
  15.             } 
  16.         } 
  17.         // 设置请求的编码格式 
  18.         httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); 
  19.         // 登录一遍 
  20.         httpclient.execute(httpost); 
  21.         // 然后再第二次请求普通的url即可。 
  22.         httpost = new HttpPost(url2); 
  23.         BasicResponseHandler responseHandler = new BasicResponseHandler(); 
  24.         System.out.println(httpclient.execute(httpost, responseHandler)); 
  25.     } finally { 
  26.         // 关闭请求 
  27.         httpclient.getConnectionManager().shutdown(); 
  28.     } 
  29.     return ""; 
			String url2) throws Exception {
		DefaultHttpClient httpclient = new DefaultHttpClient(
				new ThreadSafeClientConnManager());
		try {

			HttpPost httpost = new HttpPost(url);
			// 添加参数
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			if (params != null && params.keySet().size() > 0) {
				Iterator iterator = params.entrySet().iterator();
				while (iterator.hasNext()) {
					Map.Entry entry = (Entry) iterator.next();
					nvps.add(new BasicNameValuePair((String) entry.getKey(),
							(String) entry.getValue()));
				}
			}
			// 设置请求的编码格式
			httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
			// 登录一遍
			httpclient.execute(httpost);
			// 然后再第二次请求普通的url即可。
			httpost = new HttpPost(url2);
			BasicResponseHandler responseHandler = new BasicResponseHandler();
			System.out.println(httpclient.execute(httpost, responseHandler));
		} finally {
			// 关闭请求
			httpclient.getConnectionManager().shutdown();
		}
		return "";
	}

5.下载文件,例如mp3等等。

Java代码

  1. //第一个参数,网络连接;第二个参数,保存到本地文件的地址 
  2. public void getFile(String url, String fileName) { 
  3.         HttpClient httpClient = new DefaultHttpClient(); 
  4.         HttpGet get = new HttpGet(url); 
  5.         try { 
  6.             ResponseHandler<byte[]> handler =new ResponseHandler<byte[]>() { 
  7.                 public byte[] handleResponse(HttpResponse response) 
  8.                         throws ClientProtocolException, IOException { 
  9.                     HttpEntity entity = response.getEntity(); 
  10.                     if (entity !=
    null) { 
  11.                         return EntityUtils.toByteArray(entity); 
  12.                     } else { 
  13.                         return
    null; 
  14.                     } 
  15.                 } 
  16.             }; 
  17.  
  18.             byte[] charts = httpClient.execute(get, handler); 
  19.             FileOutputStream out = new FileOutputStream(fileName); 
  20.             out.write(charts); 
  21.             out.close(); 
  22.  
  23.         } catch (Exception e) { 
  24.             e.printStackTrace(); 
  25.         } finally { 
  26.             httpClient.getConnectionManager().shutdown(); 
  27.         } 
  28.     } 
//第一个参数,网络连接;第二个参数,保存到本地文件的地址
public void getFile(String url, String fileName) {
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);
		try {
			ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
				public byte[] handleResponse(HttpResponse response)
						throws ClientProtocolException, IOException {
					HttpEntity entity = response.getEntity();
					if (entity != null) {
						return EntityUtils.toByteArray(entity);
					} else {
						return null;
					}
				}
			};

			byte[] charts = httpClient.execute(get, handler);
			FileOutputStream out = new FileOutputStream(fileName);
			out.write(charts);
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
	}

6.创建一个多线程环境下面可用的httpClient

(原文:http://blog.csdn.net/jiaoshi0531/article/details/6459468

Java代码

  1.               HttpParams params = new BasicHttpParams(); 
  2. //设置允许链接的做多链接数目 
  3. ConnManagerParams.setMaxTotalConnections(params, 200); 
  4. //设置超时时间. 
  5. ConnManagerParams.setTimeout(params, 10000); 
  6. //设置每个路由的最多链接数量是20 
  7. ConnPerRouteBean connPerRoute = new ConnPerRouteBean(20); 
  8. //设置到指定主机的路由的最多数量是50 
  9. HttpHost localhost = new HttpHost("127.0.0.1",80); 
  10. connPerRoute.setMaxForRoute(new HttpRoute(localhost),50); 
  11. ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute); 
  12. //设置链接使用的版本 
  13. HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
  14. //设置链接使用的内容的编码 
  15. HttpProtocolParams.setContentCharset(params, 
  16.         HTTP.DEFAULT_CONTENT_CHARSET); 
  17. //是否希望可以继续使用. 
  18. HttpProtocolParams.setUseExpectContinue(params, true); 
  19.  
  20. SchemeRegistry schemeRegistry = new SchemeRegistry(); 
  21. schemeRegistry.register(new Scheme("http",PlainSocketFactory.getSocketFactory(),80)); 
  22. schemeRegistry.register(new Scheme("https",SSLSocketFactory.getSocketFactory(),443)); 
  23. ClientConnectionManager cm = new ThreadSafeClientConnManager(params,schemeRegistry); 
  24. httpClient = new DefaultHttpClient(cm, params);  
                HttpParams params = new BasicHttpParams();
		//设置允许链接的做多链接数目
		ConnManagerParams.setMaxTotalConnections(params, 200);
		//设置超时时间.
		ConnManagerParams.setTimeout(params, 10000);
		//设置每个路由的最多链接数量是20
		ConnPerRouteBean connPerRoute = new ConnPerRouteBean(20);
		//设置到指定主机的路由的最多数量是50
		HttpHost localhost = new HttpHost("127.0.0.1",80);
		connPerRoute.setMaxForRoute(new HttpRoute(localhost), 50);
		ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
		//设置链接使用的版本
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		//设置链接使用的内容的编码
		HttpProtocolParams.setContentCharset(params,
				HTTP.DEFAULT_CONTENT_CHARSET);
		//是否希望可以继续使用.
		HttpProtocolParams.setUseExpectContinue(params, true);

		SchemeRegistry schemeRegistry = new SchemeRegistry();
		schemeRegistry.register(new Scheme("http",PlainSocketFactory.getSocketFactory(),80));
		schemeRegistry.register(new Scheme("https",SSLSocketFactory.getSocketFactory(),443));
		ClientConnectionManager cm = new ThreadSafeClientConnManager(params,schemeRegistry);
		httpClient = new DefaultHttpClient(cm, params); 

7.实用的一个对象,http上下文,可以从这个对象里面取到一次请求相关的信息,例如request,response,代理主机等。

Java代码

  1. public staticvoid getUrl(String url, String encoding) 
  2.             throws ClientProtocolException, IOException { 
  3.         // 设置为get取连接的方式. 
  4.         HttpGet get = new HttpGet(url); 
  5.         HttpContext localContext = new BasicHttpContext(); 
  6.         // 得到返回的response.第二个参数,是上下文,很好的一个参数! 
  7.         httpclient.execute(get, localContext); 
  8.  
  9.         // 从上下文中得到HttpConnection对象 
  10.         HttpConnection con = (HttpConnection) localContext 
  11.                 .getAttribute(ExecutionContext.HTTP_CONNECTION); 
  12.         System.out.println("socket超时时间:" + con.getSocketTimeout()); 
  13.  
  14.         // 从上下文中得到HttpHost对象 
  15.         HttpHost target = (HttpHost) localContext 
  16.                 .getAttribute(ExecutionContext.HTTP_TARGET_HOST); 
  17.         System.out.println("最终请求的目标:" + target.getHostName() +":" 
  18.                 + target.getPort()); 
  19.  
  20.         // 从上下文中得到代理相关信息. 
  21.         HttpHost proxy = (HttpHost) localContext 
  22.                 .getAttribute(ExecutionContext.HTTP_PROXY_HOST); 
  23.         if (proxy != null) 
  24.             System.out.println("代理主机的目标:" + proxy.getHostName() +":" 
  25.                     + proxy.getPort()); 
  26.  
  27.         System.out.println("是否发送完毕:" 
  28.                 + localContext.getAttribute(ExecutionContext.HTTP_REQ_SENT)); 
  29.  
  30.         // 从上下文中得到HttpRequest对象 
  31.         HttpRequest request = (HttpRequest) localContext 
  32.                 .getAttribute(ExecutionContext.HTTP_REQUEST); 
  33.         System.out.println("请求的版本:" + request.getProtocolVersion()); 
  34.         Header[] headers = request.getAllHeaders(); 
  35.         System.out.println("请求的头信息: "); 
  36.         for (Header h : headers) { 
  37.             System.out.println(h.getName() + "--" + h.getValue()); 
  38.         } 
  39.         System.out.println("请求的链接:" + request.getRequestLine().getUri()); 
  40.  
  41.         // 从上下文中得到HttpResponse对象 
  42.         HttpResponse response = (HttpResponse) localContext 
  43.                 .getAttribute(ExecutionContext.HTTP_RESPONSE); 
  44.         HttpEntity entity = response.getEntity(); 
  45.         if (entity != null) { 
  46.             System.out.println("返回结果内容编码是:" + entity.getContentEncoding()); 
  47.             System.out.println("返回结果内容类型是:" + entity.getContentType()); 
  48.             dump(entity, encoding); 
  49.         } 
  50.     } 
public static void getUrl(String url, String encoding)
			throws ClientProtocolException, IOException {
		// 设置为get取连接的方式.
		HttpGet get = new HttpGet(url);
		HttpContext localContext = new BasicHttpContext();
		// 得到返回的response.第二个参数,是上下文,很好的一个参数!
		httpclient.execute(get, localContext);

		// 从上下文中得到HttpConnection对象
		HttpConnection con = (HttpConnection) localContext
				.getAttribute(ExecutionContext.HTTP_CONNECTION);
		System.out.println("socket超时时间:" + con.getSocketTimeout());

		// 从上下文中得到HttpHost对象
		HttpHost target = (HttpHost) localContext
				.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
		System.out.println("最终请求的目标:" + target.getHostName() + ":"
				+ target.getPort());

		// 从上下文中得到代理相关信息.
		HttpHost proxy = (HttpHost) localContext
				.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
		if (proxy != null)
			System.out.println("代理主机的目标:" + proxy.getHostName() + ":"
					+ proxy.getPort());

		System.out.println("是否发送完毕:"
				+ localContext.getAttribute(ExecutionContext.HTTP_REQ_SENT));

		// 从上下文中得到HttpRequest对象
		HttpRequest request = (HttpRequest) localContext
				.getAttribute(ExecutionContext.HTTP_REQUEST);
		System.out.println("请求的版本:" + request.getProtocolVersion());
		Header[] headers = request.getAllHeaders();
		System.out.println("请求的头信息: ");
		for (Header h : headers) {
			System.out.println(h.getName() + "--" + h.getValue());
		}
		System.out.println("请求的链接:" + request.getRequestLine().getUri());

		// 从上下文中得到HttpResponse对象
		HttpResponse response = (HttpResponse) localContext
				.getAttribute(ExecutionContext.HTTP_RESPONSE);
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			System.out.println("返回结果内容编码是:" + entity.getContentEncoding());
			System.out.println("返回结果内容类型是:" + entity.getContentType());
			dump(entity, encoding);
		}
	}

输出结果大致如下:

Txt代码

  1. socket超时时间:0 
  2. 最终请求的目标:money.finance.sina.com.cn:-1 
  3. 是否发送完毕:true 
  4. 请求的版本:HTTP/1.1 
  5. 请求的头信息:  
  6. Host--money.finance.sina.com.cn 
  7. Connection--Keep-Alive 
  8. User-Agent--Apache-HttpClient/4.2.2 (java1.5) 
  9. 请求的链接:/corp/go.php/vFD_BalanceSheet/stockid/600031/ctrl/part/displaytype/4.phtml 
  10. 返回结果内容编码是:null 
  11. 返回结果内容类型是:Content-Type: text/html 
socket超时时间:0
最终请求的目标:money.finance.sina.com.cn:-1
是否发送完毕:true
请求的版本:HTTP/1.1
请求的头信息:
Host--money.finance.sina.com.cn
Connection--Keep-Alive
User-Agent--Apache-HttpClient/4.2.2 (java 1.5)
请求的链接:/corp/go.php/vFD_BalanceSheet/stockid/600031/ctrl/part/displaytype/4.phtml
返回结果内容编码是:null
返回结果内容类型是:Content-Type: text/html

8.设置代理

Java代码

  1.              //String  hostIp代理主机ip,int port  代理端口 
  2. tpHost proxy = new HttpHost(hostIp, port); 
  3. // 设置代理主机. 
  4. tpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, 
  5. proxy); 
               //String  hostIp代理主机ip,int port  代理端口
HttpHost proxy = new HttpHost(hostIp, port);
		// 设置代理主机.
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
		proxy);

9.设置保持链接时间

Java代码

  1. //在服务端设置一个保持持久连接的特性. 
  2.         //HTTP服务器配置了会取消在一定时间内没有活动的链接,以节省系统的持久性链接资源. 
  3.         httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { 
  4.             public long getKeepAliveDuration(HttpResponse response, 
  5.                     HttpContext context) { 
  6.                 HeaderElementIterator it = new BasicHeaderElementIterator( 
  7.                         response.headerIterator(HTTP.CONN_KEEP_ALIVE)); 
  8.                 while (it.hasNext()) { 
  9.                     HeaderElement he = it.nextElement(); 
  10.                     String param = he.getName(); 
  11.                     String value = he.getValue(); 
  12.                     if (value !=
    null && param.equalsIgnoreCase("timeout")) { 
  13.                         try { 
  14.                             return Long.parseLong(value) *1000; 
  15.                         } catch (Exception e) { 
  16.  
  17.                         } 
  18.                     } 
  19.                 } 
  20.                 HttpHost target = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); 
  21.                 if("www.baidu.com".equalsIgnoreCase(target.getHostName())){ 
  22.                     return 5*1000; 
  23.                 } 
  24.                 else 
  25.                     return 30*1000;  
  26.             }  
  27.         }); 
//在服务端设置一个保持持久连接的特性.
		//HTTP服务器配置了会取消在一定时间内没有活动的链接,以节省系统的持久性链接资源.
		httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
			public long getKeepAliveDuration(HttpResponse response,
					HttpContext context) {
				HeaderElementIterator it = new BasicHeaderElementIterator(
						response.headerIterator(HTTP.CONN_KEEP_ALIVE));
				while (it.hasNext()) {
					HeaderElement he = it.nextElement();
					String param = he.getName();
					String value = he.getValue();
					if (value != null && param.equalsIgnoreCase("timeout")) {
						try {
							return Long.parseLong(value) * 1000;
						} catch (Exception e) {

						}
					}
				}
				HttpHost target = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
				if("www.baidu.com".equalsIgnoreCase(target.getHostName())){
					return 5*1000;
				}
				else
					return 30*1000;
			}
		});

转账注明出处:http://renjie120.iteye.com/blog/1727933

时间: 2024-09-16 17:18:13

HttpClient4.2.2的几个常用方法,登录之后访问页面问题,下载文件的相关文章

java-登录前和登录成功的页面Url一样 怎么回事,求大神

问题描述 登录前和登录成功的页面Url一样 怎么回事,求大神 5C 为什么登录界面的Url是****/loginaction.action,登录成功后的页面也是****/loginaction.action?前后url一样,这样我模拟登陆怎么判断是否登录成功了呢 求详解 解决方案 那就是你的代码并没有跳转呀...要是登陆成功肯定要有跳转代码的呀 解决方案二: 看你跳转代码,当你点击登录按钮 跳转的是什么页面. 你可以传一个参数过去.当登录成功,把传的参数在成功页面显示 解决方案三: 判断登陆成功

用程序登录Aps.Net页面

程序|页面 在写Internet应用程序的时候,常常需要处理用户登录的情况.一般来说,对于这种情况,我们是使用程序来模拟用户在Web页面上填写用户名.密码并提交的过程.当用户在Web页面上输入了用户名.密码并提交之后,实际上是触发了一个POST请求,在这个请求中包含有用户名.密码等信息.因此,我们只要在程序中将相关信息封装成一条POST请求,并将它发送给Web Server,基本上就能实现登录了.以MFC为例,下面的这段代码模拟了一个登录过程: CString strHeaders = _T("

在javaweb里面如果是html页面,要设计成先登录才能访问这个html页面,怎么做?

问题描述 在javaweb里面如果是html页面,要设计成先登录才能访问这个html页面,怎么做? 如题,jsp和servlet有办法,但是html页面如果不让未登录者访问? 解决方案 增加filter,检查是否有用户登录的标记. 解决方案二: DataStormSession session = DataStormSession.getInstance(); HttpServletResponse response = ServletActionContext.getResponse(); S

lrzsz-求解在win中使用secureCRT登录linux在rz上传文件时获取文件名

问题描述 求解在win中使用secureCRT登录linux在rz上传文件时获取文件名 简单来说我就是想实现,我使用rz上传脚本完成后对这个脚本文件进行处理,比如把里面的"test"改成"true" 解决方案 http://tieba.baidu.com/p/4338828408

asp net实现跨站登录-asp.net实现跨站登录,并打开登录后的页面

问题描述 asp.net实现跨站登录,并打开登录后的页面 我现在做一个网站,集成多种系统一键登录.第三方系统我无权修改. 现在我用httpwebrequest模拟登录获取到登录成功后的Cookie,打开系统登录后的页面. 使用response输出Cookie,不能修改Cookie的Domain,第三方系统无法识别到Cookie 求求大神给个完美的解决方案吧. 解决方案 不同的域名没法跨域,你说你的第三方系统不能修改,这是办不到的. 解决方案二: 如果你的客户端使用的是windows,一个变通的办

php-shopnc整个网站改为要先登录才能访问,不登陆就只能显示登陆页面

问题描述 shopnc整个网站改为要先登录才能访问,不登陆就只能显示登陆页面 15C shopnc整个网站改为要先登录才能访问,不登陆就只能显示登陆页面这个是例子:http://jpcg.pdjp.cn/index.php?act=login图片说明:! 解决方案 处理登录成功时,产生并保存一个session值,之后的页面检查该seesion是否存在并正确,不正确则重定向到登录页面. 解决方案二: 用session方法 解决方案三: 给所有控制器继承CommonController,然后用ses

session登陆验证,没有登录返回登陆页面,登陆过直接跳转

问题描述 session登陆验证,没有登录返回登陆页面,登陆过直接跳转 各位大神 我是一个新手,我希望能够得到你们的帮助. 我在写一个页面 那个页面跳到下一个页面时候需要验证是否登陆过,没有登陆直接返回登陆页面.如果登陆过了 就可以进去.能教教我吗.最好能详细点.我知道这个思路,不知道怎么实现. 解决方案 规范点的操作,配置拦截器.需要权限操作的就判断有没有获取标志位(可以用session),有就进入,没有就跳转登录页面,登录之后设置标志位. 解决方案二: 用你使用的语言/框架和"session

请问如何利用Java实现自动下载文件:此文件需要输入用户名及密码登录后才能下载。

问题描述 如题,谢谢! 解决方案 解决方案二:不明白什么是自动下载.解决方案三:在你点击的同时判断范围内是否存在登陆用户的信息..有则下没有则提示解决方案四:做一个文件下载的servletservlet中设置response的类型例如:res.setContentType("application/octet-stream;charset=UTF-8");res.setHeader("Content-Disposition","attachment;fil

急求!android登录密码验证页面源代码

问题描述 急求!android登录密码验证页面源代码 急求!android登录密码验证页面源代码,1369793795@qq.com 解决方案 <?xml version="1.0" encoding="utf-8"?> android:layout_width="match_parent" android:layout_height="match_parent" android:background="