↓簡単な使いかた↓
Http http = new Http();
Http.DataSet[] dataSet = new Http.DataSet[2];
dataSet[0] = http.new DataSet("name1","value1");
dataSet[1] = http.new DataSet("name2","value2");
String response = new String(http.post("http://hogehoge.jp",dataSet,Http.X_WWW_FORM_URLENCODED));
//適宜例外処理が必要
↓JADファイルに記述する「MIDlet-Permissions」↓
MIDlet-Permissions: javax.microedition.io.Connector.http
↓ソース「Http」クラス↓
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
public final class Http {
private static final int DEFAULT_READ_BUFFER = 1024;
private final int readBuffer;
private static Object lockObject;
static {
lockObject = new Object();
}
public Http(){
this(DEFAULT_READ_BUFFER);
}
public Http(int readBuffer){
this.readBuffer = readBuffer;
}
public byte[] get(String url) throws IOException{
byte[] data;
synchronized(lockObject){
HttpConnection httpConnection =(HttpConnection)Connector.open(url,Connector.READ,true);
InputStream is = httpConnection.openInputStream();
data=new byte[readBuffer];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int size;
while ((size=is.read(data))!=-1) {
baos.write(data,0,size);
}
baos.flush();
baos.close();
data = baos.toByteArray();
is.close();
httpConnection.close();
}
return data;
}
public byte[] get(String url,DataSet[] dataSet) throws IOException{
for(int i=0;i<dataSet.length;i++){
if(dataSet[i].value==null){
throw new IllegalArgumentException("dataSet["+i+"].value is null");
}
}
StringBuffer query = new StringBuffer();
query.append("?");
for(int i=0;i<dataSet.length;i++){
query.append(encode(dataSet[i].name));
query.append("=");
query.append(encode(dataSet[i].value));
if (i+1<dataSet.length){
query.append("&");
}
}
return get(url+query.toString());
}
public static final int X_WWW_FORM_URLENCODED=1;
public static final int MULTIPART_FORM_DATA=2;
public byte[] post(String url,DataSet[] dataSet,int contentType) throws IOException{
if(contentType==X_WWW_FORM_URLENCODED){
return postApplicationXWwwFormUrlencoded(url, dataSet);
}else if(contentType==MULTIPART_FORM_DATA){
return postMultipartFormData(url, dataSet);
}else{
return null;
}
}
private byte[] postApplicationXWwwFormUrlencoded(String url,DataSet[] dataSet) throws IOException{
for(int i=0;i<dataSet.length;i++){
if(dataSet[i].value==null){
throw new IllegalArgumentException("dataSet["+i+"].value is null");
}
}
byte[] data;
synchronized(lockObject){
HttpConnection httpConnection =(HttpConnection)Connector.open(url,Connector.READ_WRITE,true);
httpConnection.setRequestMethod(HttpConnection.POST);
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStream os = httpConnection.openOutputStream();
StringBuffer sb = new StringBuffer();
for (int i=0;i<dataSet.length;i++){
sb.append(encode(dataSet[i].name));
sb.append("=");
sb.append(encode(dataSet[i].value));
if(i+1<dataSet.length){
sb.append("&");
}
}
os.write(sb.toString().getBytes());
//os.flush();
//
//http://blog.goo.ne.jp/katsuhiromihara/e/09e61f045d39a6a297f43c66c40a1571
//902T, 903T, 803T, 904T, 705T, 910T, 810T, 811T, 813T, 812T, 911T, 814T, 815T, 912T の場合
//
//* POST データが 2016Bytes 以上の場合は、自動的にヘッダフィールドオプションを「Transfer-Encoding:chunked」にして送信する
//* POST データが 2016Bytes 以下の場合は、Content-Length を付与して送信する。ただし、flash() をコールした場合はデータサイズによらず chunked 形式で送信する
//
//705P, 706P の場合
//
//* POST データが 2560Bytes を超える場合、chunked 形式で送信する場合がある。アプリからの flash() のコールの有無には依存しない
InputStream is = httpConnection.openInputStream();
data=new byte[readBuffer];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int size;
while ((size=is.read(data))!=-1) {
baos.write(data,0,size);
}
baos.flush();
baos.close();
data = baos.toByteArray();
is.close();
httpConnection.close();
}
return data;
}
private byte[] postMultipartFormData(String url,DataSet[] dataSet)throws IOException{
String boundary = "---------------------------7d7cd255030e";
String contentType ="multipart/form-data; boundary="+ boundary;
String textBegin = "--"+ boundary + "\r\nContent-Disposition: form-data; name=\"";
String textEnd = "\"\r\n\r\n";
String fileBegin = "--"+ boundary + "\r\nContent-Disposition: form-data; name=\"";
String fileMiddle1 = "\"; filename=\"";
String fileMiddle2 = "\"\r\nContent-Type: ";
String fileEnd = "\r\n\r\n";
byte[] data;
synchronized(lockObject){
HttpConnection httpConnection =(HttpConnection)Connector.open(url,Connector.READ_WRITE);
httpConnection.setRequestMethod(HttpConnection.POST);
httpConnection.setRequestProperty("Content-Type",contentType );
OutputStream os = httpConnection.openOutputStream();
for (int i=0;i<dataSet.length;i++){
if (dataSet[i].value!=null){
StringBuffer sb = new StringBuffer();
sb.append(textBegin);
sb.append(dataSet[i].name);
sb.append(textEnd);
sb.append(dataSet[i].value);
os.write(sb.toString().getBytes());
os.write("\r\n".getBytes());
}else{
StringBuffer sb = new StringBuffer();
sb.append(fileBegin);
sb.append(dataSet[i].name);
sb.append(fileMiddle1);
sb.append(dataSet[i].filename);
sb.append(fileMiddle2);
sb.append(dataSet[i].contentType);
sb.append(fileEnd);
os.write(sb.toString().getBytes());
os.write(dataSet[i].data);
os.write("\r\n".getBytes());
}
}
//os.flush();
InputStream is = httpConnection.openInputStream();
data=new byte[readBuffer];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int size;
while ((size=is.read(data))!=-1) {
baos.write(data,0,size);
}
baos.flush();
baos.close();
data = baos.toByteArray();
is.close();
httpConnection.close();
}
return data;
}
class DataSet{
public final String name;
public final String value;
public final String filename;
public final String contentType;
public final byte[] data;
public DataSet(String name,String value){
if(name==null){
throw new IllegalArgumentException("name is null");
}
if(value==null){
throw new IllegalArgumentException("value is null");
}
this.name = name;
this.value = value;
this.filename = null;
this.contentType = null;
this.data = null;
}
public DataSet(String name,String filename,String contentType,byte[] data){
if (name == null){
throw new IllegalArgumentException("name is null");
}
if (filename == null){
throw new IllegalArgumentException("filename is null");
}
if (contentType == null){
throw new IllegalArgumentException("contentType is null");
}
if (data == null){
throw new IllegalArgumentException("data is null");
}
this.name = name;
this.value = null;
this.filename = filename;
this.contentType = contentType;
this.data = data;
}
}
//↓元のソース
//http://sdc.sun.co.jp/news/200301/ktaijava.html
//http://sdc.sun.co.jp/news/200301/dbtestformjava.html
private static String encode(String in) {
StringBuffer inBuf = new StringBuffer(in);
StringBuffer outBuf = new StringBuffer();
for (int i = 0; i < inBuf.length(); i++) {
char temp = inBuf.charAt(i);
if (('a' <= temp && temp <= 'z') || ('A' <= temp && temp <= 'Z') || ('0' <= temp && temp <= '9') || temp == '.' || temp == '-' || temp == '*' || temp == '_') {
outBuf.append(temp);
} else if (temp == ' ') {
outBuf.append('+');
} else {
byte[] bytes;
try {
bytes = new String(new char[]{temp}).getBytes("SJIS");
for (int j = 0; j < bytes.length; j++) {
int high = (bytes[j] >>> 4) & 0x0F;
int low = (bytes[j] & 0x0F);
outBuf.append('%');
outBuf.append(Integer.toString(high, 16).toUpperCase());
outBuf.append(Integer.toString(low, 16).toUpperCase());
}
} catch (Exception e) {
}
}
}
return outBuf.toString();
}
}