Skip to content

Instantly share code, notes, and snippets.

@anta40
Forked from amrishodiq/HttpClient.java
Created May 22, 2013 10:48
Show Gist options
  • Save anta40/5626718 to your computer and use it in GitHub Desktop.
Save anta40/5626718 to your computer and use it in GitHub Desktop.
/**
* Copyright (c) E.Y. Baskoro, Research In Motion Limited.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* This License shall be included in all copies or substantial
* portions of the Software.
*
* The name(s) of the above copyright holders shall not be used
* in advertising or otherwise to promote the sale, use or other
* dealings in this Software without prior written authorization.
*
*
*/
package com.durianapp.bbcommons.helper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.io.HttpConnection;
import net.rim.blackberry.api.browser.PostData;
import net.rim.blackberry.api.browser.URLEncodedPostData;
import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.io.http.HttpProtocolConstants;
import net.rim.device.api.io.transport.ConnectionDescriptor;
import net.rim.device.api.io.transport.ConnectionFactory;
import net.rim.device.api.io.transport.TransportInfo;
import net.rim.device.api.io.transport.options.BisBOptions;
import net.rim.device.api.io.transport.options.TcpCellularOptions;
import net.rim.device.api.system.GPRSInfo;
public class HttpClient {
public static HttpClient http;
public static HttpClient get() {
http = new HttpClient(new ConnectionFactory());
return http;
}
protected ConnectionFactory cf;
protected static final String BOUNDARY = "----------V2ymHFg03ehbqgZCaKO6jy";
protected static final String START_BOUNDARY = "--" + BOUNDARY + "\r\n";
protected static final String END_BOUNDARY = "\r\n--" + BOUNDARY + "--\r\n";
public HttpClient(ConnectionFactory pcf) {
cf = pcf;
int[] preferredTransportTypes;
preferredTransportTypes = new int[] {
TransportInfo.TRANSPORT_TCP_WIFI,
TransportInfo.TRANSPORT_BIS_B,
TransportInfo.TRANSPORT_MDS,
TransportInfo.TRANSPORT_TCP_CELLULAR
};
cf.setPreferredTransportTypes(preferredTransportTypes);
BisBOptions bisOptions = new BisBOptions("mds-public");
cf.setTransportTypeOptions(TransportInfo.TRANSPORT_BIS_B, bisOptions);
// TcpCellularOptions tcpOptions = new TcpCellularOptions();
// if (GPRSInfo.getHomeMCC() == 510 && GPRSInfo.getHomeMNC() == 8) {
// tcpOptions.setApn("AXIS");
// tcpOptions.setTunnelAuthUsername("axis");
// tcpOptions.setTunnelAuthPassword("123456");
// }
// cf.setTransportTypeOptions(TransportInfo.TRANSPORT_TCP_CELLULAR, tcpOptions);
}
public StringBuffer doGet(String url, Hashtable args) throws Exception {
StringBuffer urlBuffer = new StringBuffer(url);
if ((args != null) && (args.size() > 0)) {
urlBuffer.append('?');
Enumeration keysEnum = args.keys();
while (keysEnum.hasMoreElements()) {
String key = (String) keysEnum.nextElement();
String val = (String) args.get(key);
urlBuffer.append(key).append('=').append(val);
if (keysEnum.hasMoreElements()) {
urlBuffer.append('&');
}
}
}
return doGet(urlBuffer.toString());
}
public StringBuffer doGet(String url) throws Exception {
HttpConnection conn = null;
StringBuffer buffer = new StringBuffer();
try {
if ((url == null) || url.equalsIgnoreCase("") || (cf == null)) {
return null;
}
System.out.println("URL: "+url);
ConnectionDescriptor connd = cf.getConnection(url);
if (connd != null)
System.out.println("Connd is not null");
else {
System.out.println("Connd is null ");
throw new Exception("Server is unreachable. Please try again later.");
}
String transportTypeName = TransportInfo.getTransportTypeName(connd
.getTransportDescriptor().getTransportType());
System.out.println("TTN: "+transportTypeName);
conn = (HttpConnection) connd.getConnection();
int resCode = conn.getResponseCode();
String resMessage = conn.getResponseMessage();
System.out.println("RC: "+resCode);
System.out.println("RM: "+resMessage);
switch (resCode) {
case HttpConnection.HTTP_OK: {
InputStream inputStream = conn.openInputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
buffer.append(new String(bytes, 0, len));
}
inputStream.close();
break;
}
case HttpConnection.HTTP_BAD_REQUEST: {
InputStream inputStream = conn.openInputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
buffer.append(new String(bytes, 0, len));
}
inputStream.close();
break;
}
case HttpConnection.HTTP_TEMP_REDIRECT:
case HttpConnection.HTTP_MOVED_TEMP:
case HttpConnection.HTTP_MOVED_PERM: {
url = conn.getHeaderField("Location");
buffer = doGet(url);
break;
}
default:
break;
}
} catch (Exception e) {
System.out.println("Ex: "+e.getMessage());
throw e;
} catch (Throwable e) {
System.out.println("T: "+e.getMessage());
throw new Exception(e.getMessage());
} finally {
if (conn != null) {
try {
conn.close();
} catch (IOException e) {
}
}
}
return buffer;
}
public StringBuffer doPost(String url, Hashtable data) throws Exception {
URLEncodedPostData encoder = new URLEncodedPostData("UTF-8", false);
Enumeration keysEnum = data.keys();
while (keysEnum.hasMoreElements()) {
String key = (String) keysEnum.nextElement();
String val = (String) data.get(key);
System.out.println("- " + key + ": " + val);
encoder.append(key, val);
}
return doPost(url, encoder);
}
public StringBuffer doPost(String url, PostData postData) throws Exception {
HttpConnection conn = null;
OutputStream os = null;
StringBuffer buffer = new StringBuffer();
try {
if ((url == null) || url.equalsIgnoreCase("") || (cf == null)) {
return null;
}
ConnectionDescriptor connd = cf.getConnection(url);
String transportTypeName = TransportInfo.getTransportTypeName(connd
.getTransportDescriptor().getTransportType());
conn = (HttpConnection) connd.getConnection();
if (conn != null) {
try {
// post data
if (postData != null) {
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty(
HttpProtocolConstants.HEADER_CONTENT_TYPE,
postData.getContentType());
conn.setRequestProperty(
HttpProtocolConstants.HEADER_CONTENT_LENGTH,
String.valueOf(postData.size()));
os = conn.openOutputStream();
os.write(postData.getBytes());
} else {
conn.setRequestMethod(HttpConnection.GET);
}
} catch (Throwable t) {
t.printStackTrace();
}
int resCode = conn.getResponseCode();
String resMessage = conn.getResponseMessage();
switch (resCode) {
case HttpConnection.HTTP_OK: {
InputStream inputStream = conn.openInputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
buffer.append(new String(bytes, 0, len));
}
inputStream.close();
break;
}
case HttpConnection.HTTP_BAD_REQUEST: {
InputStream inputStream = conn.openInputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
buffer.append(new String(bytes, 0, len));
}
inputStream.close();
break;
}
case HttpConnection.HTTP_TEMP_REDIRECT:
case HttpConnection.HTTP_MOVED_TEMP:
case HttpConnection.HTTP_MOVED_PERM: {
url = conn.getHeaderField("Location");
buffer = doPost(url, postData);
break;
}
default:
break;
}
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (IOException e) {
}
}
}
return buffer;
}
public StringBuffer doPostMultipart(String url, Hashtable params,
String name, String fileName, String fileType, byte[] payload)
throws Exception {
HttpConnection conn = null;
OutputStream os = null;
StringBuffer buffer = new StringBuffer();
try {
if ((url == null) || url.equalsIgnoreCase("") || (cf == null)) {
return null;
}
ConnectionDescriptor connd = cf.getConnection(url);
String transportTypeName = TransportInfo.getTransportTypeName(connd
.getTransportDescriptor().getTransportType());
conn = (HttpConnection) connd.getConnection();
if (conn != null) {
try {
byte[] postBytes = getMultipartPostBytes(params, name,
fileName, fileType, payload);
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty(
HttpProtocolConstants.HEADER_CONTENT_TYPE,
HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA
+ "; boundary=" + BOUNDARY);
conn.setRequestProperty(
HttpProtocolConstants.HEADER_CONTENT_LENGTH,
postBytes + "");
os = conn.openOutputStream();
os.write(postBytes);
} catch (Throwable t) {
t.printStackTrace();
}
int resCode = conn.getResponseCode();
String resMessage = conn.getResponseMessage();
switch (resCode) {
case HttpConnection.HTTP_OK: {
InputStream inputStream = conn.openInputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
buffer.append(new String(bytes, 0, len));
}
inputStream.close();
break;
}
case HttpConnection.HTTP_BAD_REQUEST: {
InputStream inputStream = conn.openInputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
buffer.append(new String(bytes, 0, len));
}
inputStream.close();
break;
}
case HttpConnection.HTTP_TEMP_REDIRECT:
case HttpConnection.HTTP_MOVED_TEMP:
case HttpConnection.HTTP_MOVED_PERM: {
url = conn.getHeaderField("Location");
buffer = doPostMultipart(url, params, name, fileName,
fileType, payload);
break;
}
default:
break;
}
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (IOException e) {
}
}
}
return buffer;
}
protected byte[] getMultipartPostBytes(Hashtable params, String name,
String fileName, String fileType, byte[] payload) {
StringBuffer res = new StringBuffer(START_BOUNDARY);
Enumeration keys = params.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String value = (String) params.get(key);
res.append("Content-Disposition: form-data; name=\"").append(key)
.append("\"\r\n").append("\r\n").append(value)
.append("\r\n").append("--").append(BOUNDARY)
.append("\r\n");
}
res.append("Content-Disposition: form-data; name=\"").append(name)
.append("\"; filename=\"").append(fileName).append("\"\r\n")
.append("Content-Type: ").append(fileType).append("\r\n\r\n");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
baos.write(res.toString().getBytes());
baos.write(payload);
baos.write(END_BOUNDARY.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
return baos.toByteArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment