1. /*
  2. * Copyright 1999,2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package peaklau.eaglefund;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import javax.microedition.io.Connector;
  21. import javax.microedition.io.HttpConnection;
  22. /**
  23. * HttpClient class<br>
  24. * do http get or post<br>
  25. *
  26. * <br>
  27. * @author peaklau <br>
  28. * email:<A HREF="mailto:peaklau@hotmail.com">peaklau@hotmail.com</A> <br>
  29. * <a href="http://www.peaklau.com/fund/english/">HomePage</a>
  30. * @version $Revision: 1.2 $ $Date: 2007/10/21 14:26:24 $
  31. */
  32. public class HttpClient {
  33. public static final int BUFFER_SIZE=1024 *10;
  34. private static final String TYPE_TEXT="text/html";
  35. private static final String TYPE_IMAGE="image/png";
  36. public static int CONTENT_TYPE_TEXT = 0;
  37. public static int CONTENT_TYPE_IMAGE = 1;
  38. private byte[] buf = new byte[BUFFER_SIZE];
  39. private int contentType = 0;
  40. private int size = 0;
  41. private String currentURL;
  42. public synchronized void httpGet(String url) throws Exception {
  43. httpGetOrPost(url,null);
  44. }
  45. public synchronized void httpPost(String url,String postData) throws Exception {
  46. httpGetOrPost(url,postData);
  47. }
  48. public int getContentType() {
  49. return contentType;
  50. }
  51. public byte[] getData() {
  52. return buf;
  53. }
  54. public int getSize() {
  55. return size;
  56. }
  57. private void httpGetOrPost(String url, String postData)throws Exception {
  58. currentURL = url;
  59. if (url.indexOf('/', 7) == -1) {
  60. url = url + "/";
  61. }
  62. HttpConnection c = null;
  63. InputStream is = null;
  64. OutputStream os = null;
  65. try {
  66. int status = -1;
  67. // If there are more than n redirects, give up, we might
  68. // be in a loop.
  69. int redirects =3;
  70. c = (HttpConnection) Connector.open(url, Connector.READ_WRITE, true);
  71. setRequestHeaders(c);
  72. if (postData != null) {
  73. c.setRequestMethod(HttpConnection.POST);
  74. os = c.openOutputStream();
  75. os.write(postData.getBytes());
  76. os.flush();
  77. }
  78. while (true) {
  79. status = c.getResponseCode();
  80. currentURL = c.getURL();
  81. if (status == HttpConnection.HTTP_TEMP_REDIRECT
  82. || status == HttpConnection.HTTP_MOVED_TEMP
  83. || status == HttpConnection.HTTP_MOVED_PERM) {
  84. // Get the new location and close the connection
  85. url = c.getHeaderField("location");
  86. if (is != null) {
  87. is.close();
  88. }
  89. if (os != null) {
  90. os.close();
  91. }
  92. //c.close();
  93. redirects--;
  94. if (redirects < 0) {
  95. throw new Exception("redirects exception!");
  96. }
  97. // System.out.println("Redirecting to " + url);
  98. c = (HttpConnection) Connector.open(url, Connector.READ_WRITE, true);
  99. setRequestHeaders(c);
  100. } else {
  101. break;
  102. }
  103. }
  104. // Only HTTP_OK (200) means the content is returned.
  105. if (status != HttpConnection.HTTP_OK) {
  106. throw new Exception("HTTP status="+ status);
  107. }
  108. // Get the ContentType
  109. String type = c.getType();
  110. if((type!=null)&&(type.toLowerCase().equals(TYPE_IMAGE))){
  111. contentType=CONTENT_TYPE_IMAGE;
  112. }else{
  113. contentType=CONTENT_TYPE_TEXT;;
  114. }
  115. // open the InputStream
  116. is = c.openInputStream();
  117. // Report the ContentLength
  118. long len = c.getLength();
  119. size=0;
  120. if (len > 0) {
  121. while(size<len){
  122. int tmp=is.read(buf,size,BUFFER_SIZE);
  123. size+=tmp;
  124. }
  125. }
  126. } finally {
  127. if (is != null) {
  128. is.close();
  129. }
  130. if (os != null) {
  131. os.close();
  132. }
  133. }
  134. }
  135. public void setRequestHeaders(HttpConnection c) throws IOException {
  136. //String conf = System.getProperty("microedition.configuration");
  137. //String prof = System.getProperty("microedition.profiles");
  138. //String locale = System.getProperty("microedition.locale");
  139. //c.setRequestProperty("User-Agent",conf+"/"+prof+"/fund");
  140. //c.setRequestProperty("Connection","Close");
  141. //if (locale != null) {
  142. //c.setRequestProperty("Content-Language", locale);
  143. //}
  144. }
  145. public void reflesh() throws Exception {
  146. httpGet(currentURL);
  147. }
  148. }