PostBot--Post Data via JavaME Http Connection

/*
* 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;
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java进行HTTP POST请求并使用"multipart/form-data"格式,你可以使用Apache HttpClient库。下面是一个示例代码: ```java import java.io.File; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.HttpClients; public class MultipartFormDataExample { public static void main(String[] args) throws IOException { String url = "http://example.com/upload"; String filePath = "/path/to/file.txt"; HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); // 添加文件参数 File file = new File(filePath); builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName()); HttpEntity multipart = builder.build(); httpPost.setEntity(multipart); HttpResponse response = httpClient.execute(httpPost); // 处理响应... } } ``` 在这个示例中,你需要将`url`替换为你要发送POST请求的URL,将`filePath`替换为你要上传的文件的路径。接下来,我们创建一个`HttpClient`实例和一个`HttpPost`实例。然后,我们使用`MultipartEntityBuilder`来构建"multipart/form-data"格式的请求实体,将文件添加为二进制参数。最后,我们将请求实体设置到`HttpPost`实例中,并发送请求。 请注意,这个示例使用的是Apache HttpClient 4.x版本。如果你使用的是不同的HTTP客户端库,请查阅其文档以获取相应的实现方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值