- /*
- * Copyright 1999,2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package peaklau.eaglefund;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import javax.microedition.io.Connector;
- import javax.microedition.io.HttpConnection;
- /**
- * HttpClient class<br>
- * do http get or post<br>
- *
- * <br>
- * @author peaklau <br>
- * email:<A HREF="mailto:peaklau@hotmail.com">peaklau@hotmail.com</A> <br>
- * <a href="http://www.peaklau.com/fund/english/">HomePage</a>
- * @version $Revision: 1.2 $ $Date: 2007/10/21 14:26:24 $
- */
- public class HttpClient {
- public static final int BUFFER_SIZE=1024 *10;
- private static final String TYPE_TEXT="text/html";
- private static final String TYPE_IMAGE="image/png";
- public static int CONTENT_TYPE_TEXT = 0;
- public static int CONTENT_TYPE_IMAGE = 1;
- private byte[] buf = new byte[BUFFER_SIZE];
- private int contentType = 0;
- private int size = 0;
- private String currentURL;
- public synchronized void httpGet(String url) throws Exception {
- httpGetOrPost(url,null);
- }
- public synchronized void httpPost(String url,String postData) throws Exception {
- httpGetOrPost(url,postData);
- }
- public int getContentType() {
- return contentType;
- }
- public byte[] getData() {
- return buf;
- }
- public int getSize() {
- return size;
- }
- private void httpGetOrPost(String url, String postData)throws Exception {
- currentURL = url;
- if (url.indexOf('/', 7) == -1) {
- url = url + "/";
- }
- HttpConnection c = null;
- InputStream is = null;
- OutputStream os = null;
- try {
- int status = -1;
- // If there are more than n redirects, give up, we might
- // be in a loop.
- int redirects =3;
- c = (HttpConnection) Connector.open(url, Connector.READ_WRITE, true);
- setRequestHeaders(c);
- if (postData != null) {
- c.setRequestMethod(HttpConnection.POST);
- os = c.openOutputStream();
- os.write(postData.getBytes());
- os.flush();
- }
- while (true) {
- status = c.getResponseCode();
- currentURL = c.getURL();
- if (status == HttpConnection.HTTP_TEMP_REDIRECT
- || status == HttpConnection.HTTP_MOVED_TEMP
- || status == HttpConnection.HTTP_MOVED_PERM) {
- // Get the new location and close the connection
- url = c.getHeaderField("location");
- if (is != null) {
- is.close();
- }
- if (os != null) {
- os.close();
- }
- //c.close();
- redirects--;
- if (redirects < 0) {
- throw new Exception("redirects exception!");
- }
- // System.out.println("Redirecting to " + url);
- c = (HttpConnection) Connector.open(url, Connector.READ_WRITE, true);
- setRequestHeaders(c);
- } else {
- break;
- }
- }
- // Only HTTP_OK (200) means the content is returned.
- if (status != HttpConnection.HTTP_OK) {
- throw new Exception("HTTP status="+ status);
- }
- // Get the ContentType
- String type = c.getType();
- if((type!=null)&&(type.toLowerCase().equals(TYPE_IMAGE))){
- contentType=CONTENT_TYPE_IMAGE;
- }else{
- contentType=CONTENT_TYPE_TEXT;;
- }
- // open the InputStream
- is = c.openInputStream();
- // Report the ContentLength
- long len = c.getLength();
- size=0;
- if (len > 0) {
- while(size<len){
- int tmp=is.read(buf,size,BUFFER_SIZE);
- size+=tmp;
- }
- }
- } finally {
- if (is != null) {
- is.close();
- }
- if (os != null) {
- os.close();
- }
- }
- }
- public void setRequestHeaders(HttpConnection c) throws IOException {
- //String conf = System.getProperty("microedition.configuration");
- //String prof = System.getProperty("microedition.profiles");
- //String locale = System.getProperty("microedition.locale");
- //c.setRequestProperty("User-Agent",conf+"/"+prof+"/fund");
- //c.setRequestProperty("Connection","Close");
- //if (locale != null) {
- //c.setRequestProperty("Content-Language", locale);
- //}
- }
- public void reflesh() throws Exception {
- httpGet(currentURL);
- }
- }