开源 Fetion api java

 飞信登录过程代码:

 

	public boolean login() {
		//登陆
		HttpClient client = new HttpClient();

		HttpConnectionManagerParams managerParams = client.getHttpConnectionManager().getParams();
		// 设置连接超时时间(单位毫秒) 
		managerParams.setConnectionTimeout(30000);
		// 设置读数据超时时间(单位毫秒) 
		managerParams.setSoTimeout(120000);

		InputStream is = null;
		PostMethod postMethod = null;
		Matcher matcher = null;

		boolean result = false;

		try {
			postMethod = new PostMethod(FETION_LOGIN_URL + "?mobileno=" + URLEncoder.encode(mobile, "UTF-8") + "&pwd="
					+ URLEncoder.encode(password, "UTF-8"));

			DefaultHttpMethodRetryHandler defaultHttpMethodRetryHandler = new DefaultHttpMethodRetryHandler(100, false);

			postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, defaultHttpMethodRetryHandler);

			client.executeMethod(postMethod);

			if (postMethod.getStatusCode() != HttpStatus.SC_OK) {
				System.out.println("认证失败!");
			} else {

				Header[] headers = postMethod.getResponseHeaders();
				String Set_Cookie = headers[4].getValue();
				//				System.out.println("Set_Cookie:" + Set_Cookie);
				Pattern ssicPattern = Pattern.compile("ssic=(.*?);");
				matcher = ssicPattern.matcher(Set_Cookie);

				if (matcher.find()) {
					ssic = matcher.group(1);
				}

				System.out.println("ssic:" + ssic);

				is = postMethod.getResponseBodyAsStream();
				SAXReader sr = new SAXReader();
				Document document = sr.read(is);
				// 获取根元素
				Element root = document.getRootElement();
				//				String status_code = root.attribute("status-code").getValue();
				Element user = root.element("user");
				uri = user.attribute("uri").getValue();
				user.attribute("mobile-no").getValue();
				user.attribute("user-status").getValue();

				Pattern sidPattern = Pattern.compile("sip:(\\d+)@(.+);");

				matcher = sidPattern.matcher(uri);

				if (matcher.find()) {
					sid = matcher.group(1);
					domain = matcher.group(2);
				}

				if (httpRegister()) {
					result = false;
				}
			}

		} catch (Exception e) {
			//			e.printStackTrace();
			System.out.println("进行认证异常时间TIME************" + new Timestamp(System.currentTimeMillis()));

		} finally {
			// 资源的释放
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (postMethod != null) {
				postMethod.releaseConnection();
			}
		}

		return result;
	}

 

 

你可能感兴趣的:(java,apache,.net,mobile,sun)