PostBot--Post Data via JavaME Http Connection

本文介绍了一个名为 PostBot 的 Java 类,该类用于通过 HTTP POST 方法发送数据或文件输入流。PostBot 支持设置 User-Agent、Content-Type 和 Content-Length 等请求头,并能够处理不同的数据类型如字节数组或文件流。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
* 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;
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值