/*
* PostBot.java
* Oct. 9, 2007
*/
package com.vendor.dept.midp.http;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
/**
* bot class can post given byte array data or file input stream
*
* @author
*
*/
public class PostBot {
private static final int BUF_SIZE = 1024 * 10;
private static final int ERR_OPEN_INPUT_STREAM = 910;
private static final int ERR_OPEN_OUTPUT_STREAM = 930;
private static final int ERR_WRITE_OUTPUT_STREAM = 931;
private static final String LBL_CONTENT_LENGTH = "Content-Length";
private static final String LBL_CONTENT_TYPE = "Content-Type";
private static final String LBL_USER_AGENT = "User-Agent";
public static final String TYPE_3GPP = "video/3gpp";
public static final String TYPE_JPEG = "image/jpeg";
public static final String TYPE_PNG = "image/png";
private String agent;
private HttpConnection conn;
private byte[] data;
private InputStream is;
private OutputStream os;
private boolean postAsStream;
private int responseCode;
private String responseMessage;
private String type;
private String url;
/**
* constructor of PostBot
*/
public PostBot(String URL) {
this.url = URL;
agent = "Mozilla/4.0";
data = null;
is = null;
responseCode = -1;
responseMessage = null;
type = "application/x-www-form-urlencoded";
postAsStream = false;
}
public int doPost() {
try {
conn = (HttpConnection) Connector.open(url);
if (conn == null) {
return -1;
}
conn.setRequestMethod(HttpConnection.POST);
if (agent != null && agent.trim().length() > 0) {
conn.setRequestProperty(LBL_USER_AGENT, agent);
}
if (type != null && type.trim().length() > 0) {
conn.setRequestProperty(LBL_CONTENT_TYPE, type);
}
if (data != null && data.length > 0) {
conn.setRequestProperty(LBL_CONTENT_LENGTH, String
.valueOf(data.length));
}
// open file connection
if (is == null) {
return ERR_OPEN_INPUT_STREAM;
}
// open HTTP connection
os = null;
try {
os = conn.openOutputStream();
} catch (IOException e) {
e.printStackTrace();
return ERR_OPEN_OUTPUT_STREAM;
}
if (this.postAsStream) {
doPostStream();
} else {
doPostByte();
}
//
Thread.sleep(2000);
//
this.responseCode = this.conn.getResponseCode();
this.responseMessage = this.conn.getResponseMessage();
//
return 1;
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return -1;
}
/**
* post the given byte[] data
*
* @return int of posted length
*/
private int doPostByte() {
try {
if (data != null && data.length > 0) {
os.write(data, 0, data.length);
os.flush();
}
return data.length;
} catch (IOException e) {
e.printStackTrace();
return ERR_WRITE_OUTPUT_STREAM;
}
}
/**
* post the given inputstream
*
* @return int of posted length
*/
private int doPostStream() {
// loop to post
int cursor = 0;
try {
byte[] buf = new byte[BUF_SIZE];
int rlen = is.read(buf);
while (rlen > 0) {
if (rlen < BUF_SIZE) {
// write exactly length of read to output stream
os.write(buf, 0, rlen);
} else {
os.write(buf);
}
os.flush();
cursor += rlen;
// prepare for the next loop
rlen = is.read(buf);
// next
}
return cursor;
} catch (IOException e) {
e.printStackTrace();
return ERR_WRITE_OUTPUT_STREAM;
}
}
public final int getResponseCode() {
return responseCode;
}
public final String getResponseMessage() {
return responseMessage;
}
public final void setAgent(String s) {
this.agent = s;
}
public final void setData(byte[] b) {
this.data = b;
this.postAsStream = false;
this.is = null;
}
public final void setIs(InputStream iis) {
this.is = iis;
this.postAsStream = true;
this.data = null;
}
public final void setResponseCode(int i) {
this.responseCode = i;
}
public final void setResponseMessage(String s) {
this.responseMessage = s;
}
public final void setType(String s) {
this.type = s;
}
}
-----
Usage:
1 Constructor;
2 setIs;
3 setType;
4 doPost;
PostBot--Post Data via JavaME Http Connection
最新推荐文章于 2023-02-27 13:48:49 发布