⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpclient.java

📁 开发框架。 一.说明: 此框架的意图是解决手机软件开发中常遇到
💻 JAVA
字号:
/*
 * HttpClient.java
 *
 * Created on 2006年4月28日, 下午9:16
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.gggeye.easymf.net;

 

import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Form;

import org.gggeye.easymf.log.Logger;
import org.gggeye.easymf.util.Tools;

/**
 * HttpClient is the core of the httpme framework,It provides three methods to
 * send data to web server.
 * 
 * @author mingjava
 * @version 0.1 05/06/2006
 * @since httpme 0.1
 */
public   class HttpClient {

	private String host = "";
	private CookieManager cm = new CookieManager();
	public static final String BOUNDARY = "----------------dfjksdfjkdjf88982kjdf";
	public static final String APPLICATION_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded";
	public static final String MULTIPART_FORM_DATA = "multipart/form-data";
	public static final String SET_COOKIE = "set-cookie";
	public static final String COOKIE = "cookie";
	
	String charset = "UTF-8";

	/**
	 * Initializes a HttpClient instance so that you can use it to submit the form
	 * 
	 * @param host
	 *            a String such as http://www.j2medev.com
	 */
	public HttpClient(String host) {
		this.host = host;
	}

	/**
	 * send the data to the web server,This will use GET method.and the data
	 * will be encoded in the url. for example /webapp/post?hello=world
	 * 
	 * @param uri
	 *            the uri of the request
	 * @param params
	 *            a String array ,the parameter's name
	 * @param values
	 *            a String array ,the parameter's value
	 */
	public void get(final String _uri, String[] _params, String[] _values) {
		StringBuffer tQuery = new StringBuffer("?");
		if(_params != null && _values != null && _params.length == _values.length){
			for(int i=0; i < _params.length; i++){
				tQuery.append(_params[i]);
				tQuery.append("=");
				tQuery.append(_values[i]);
				tQuery.append("&");
			}
			tQuery.deleteCharAt(_params.length-1);
		}
		
	    //String tQ = Tools.decoder(tQuery.toString());	
		
		try {
			HttpConnection tHttpConnection = (HttpConnection) Connector.open(host + tQuery.toString() );
			tHttpConnection.setRequestProperty("User-Agent", "j2me");
            tHttpConnection.setRequestProperty("Connection", "close");
            tHttpConnection.setRequestMethod("GET");
            tHttpConnection.setRequestProperty("Referer", "");
            int tResponseCode = tHttpConnection.getResponseCode();
            
            if(tResponseCode != HttpConnection.HTTP_OK){
            	this.onFailed(tResponseCode);
            }else
            	this.onSuccess(tHttpConnection);
            
		} catch (Exception e) {
			Logger.debug(e);
			this.onFailed(-1);
		}
		
	}

	/**
	 * post the data contained in the form to the web server.the content type is
	 * set to application/x-www-form-urlencoded or multipart/form-data according
	 * parameter type. you should note that if the length of data is more than
	 * 2106 bytes,the mobile device may be transfer the data block by block.so
	 * the header content-length will be discarded.The web server should be able
	 * to handle this situation.
	 * 
	 * @param uri
	 *            the uri of request
	 * @param form
	 *            the form which contained data to be submited
	 * @param type
	 *            the content-type either application/x-www-form-urlencoded or
	 *            multipart/form-data
	 */
	public void post(final String uri, final Form form, final String type)
			throws IllegalArgumentException {

		byte[] data = null;
		HttpConnection conn = null;
		try {
			conn = (HttpConnection) Connector.open(host + uri);
			conn.setRequestMethod(HttpConnection.POST);
			if (APPLICATION_X_WWW_FORM_URLENCODED.equals(type)) {
				// data = FormUtil.collect(form);
				conn.setRequestProperty("Content-type",
						APPLICATION_X_WWW_FORM_URLENCODED);
			} else if (MULTIPART_FORM_DATA.equals(type)) {
				// data = FormUtil.collectMultipart(form);
				conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA
						+ ";boundary=" + BOUNDARY);
			} else {
				throw new IllegalArgumentException(
						"content-type header is invalid");
			}
			conn.setRequestProperty("Content-Length", String
					.valueOf(data.length));
			Cookie[] cookies = cm.getCookie(uri);
			if (cookies != null) {
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < cookies.length; i++) {
					sb.append(cookies[i].getName() + "="
							+ cookies[i].getValue() + ";");
				}
				String c = sb.toString();
				// delete the last ;
				conn.setRequestProperty(COOKIE, c.substring(0, c.length() - 1));
			}
			DataOutputStream dos = conn.openDataOutputStream();
			dos.write(data);
			dos.close();
			int code = conn.getResponseCode();
			if (code != HttpConnection.HTTP_OK) {
				onFailed(code);
			}
			String sCookie = conn.getHeaderField(SET_COOKIE);
			if (sCookie != null) {
				Cookie cookie = CookieManager.parseCookie(sCookie, uri);
				cm.addCookie(cookie);
			}
			//InputStream dis = conn.openDataInputStream();
			onSuccess(conn);

		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}

	/**
	 * the subclass of HttpClient should implement this method when the response
	 * is returned successflly
	 * 
	 * @param conn
	 *            the HttpConnection instance
	 * @param dis
	 *            the DataInputStream returned from
	 *            HttpConnection.openDataInputStream();
	 */
	public   void onSuccess(HttpConnection _httpConnection){
		
	}

	/**
	 * the subclass of HttpClient should implement this method when error happend
	 * 
	 * @param code
	 *            the response code
	 */
	public   void onFailed(int code){
		
	}
	
	 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -