📄 httphandler.java
字号:
package org.httpClient.com;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import org.gui.com.HttpClient;
import org.gui.com.WMLForm;
import org.gui.com.MainForm;
import org.xmlpull.v1.XmlPullParserException;
import java.io.*;
import javax.microedition.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HttpHandler extends Thread{
private String result;
private Display display;
private String URL = null;
private String message = null;
private String method = "get";
private Date startTime = null;
private Date overTime = null;
private WMLForm arrayForm;
private int count = 0;
private int time;
private RMStore cookieClass = null;
private HttpClient hc = null;
private int number = 1;
private int sum = 0;
public HttpHandler(Display display,String URL,String message,
String method,RMStore cookieClass,HttpClient hc){
this.display = display;
this.cookieClass = cookieClass;
this.URL = URL;
this.message = message;
this.method = method;
this.hc = hc;
}
public void run() {
try{
if(method.equals("get")){
doGet();
}else{
doPost();
}
}catch(IOException ex){
ex.printStackTrace();
}
}
public HttpConnection connectHttp(String URL){
HttpConnection con = null;
try {
if (!URL.startsWith("http://") &&!URL.startsWith("https://")) {
URL = "http://" + URL;
}
con = (HttpConnection)Connector.open(URL, Connector.READ_WRITE);
return con;
} catch (Exception ex) {
System.out.println(ex);
ex.printStackTrace();
return null;
}
}
public void doGet()throws IOException{
OutputStream out = null;
InputStream in = null;
boolean ret = false;
result = "";
HttpConnection con =null;
if(message != null){
URL = URL + "?"+message;
}
startTime = new Date();
con =connectHttp(URL);
try
{
con.setRequestMethod(HttpConnection.GET);
if (cookieClass.getCookie()!= null)
con.setRequestProperty("cookie", cookieClass.getCookie());
con.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0" );
con.setRequestProperty("Content-Language", "en-US" );
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String tmpCookie = con.getHeaderField("set-cookie");
if (tmpCookie != null)
{
cookieClass.writeRecord(tmpCookie);
cookieClass.setCookie(tmpCookie);
}
in = new DataInputStream(con.openInputStream());
int ch;
while ((ch = in.read()) != -1) {
result = result + (char) ch;
}
overTime = new Date();
time =(int) (overTime.getTime()-startTime.getTime()); //算延时
}catch(IOException ex){
ex.printStackTrace();
}
finally
{
if (in!= null)
in.close();
if (con != null)
con.close();
}
try{
new ProcessWML(result,display,time,cookieClass,hc).process();
}catch(XmlPullParserException xex){
xex.printStackTrace();
}
}
public void doPost()throws IOException{
HttpConnection con = null;
DataInputStream in = null;
DataOutputStream out = null;
String result = "";
String requeststring = " "+message;
startTime = new Date();
con = connectHttp(URL);
try {
con.setRequestMethod(HttpConnection.POST);
if (cookieClass.getCookie()!= null)
con.setRequestProperty("cookie", cookieClass.getCookie());
con.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0" );
con.setRequestProperty("Content-Language", "en-US" );
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
out = con.openDataOutputStream();
byte[] request_body = null;
int j = 0;
for(int i=0;i<message.length();i++){
if(message.charAt(i)=='&'){
request_body = message.substring(j,i).getBytes();
out.write(request_body);
j = i;
}
}
request_body = message.substring(j).getBytes();
out.write(request_body);
String tmpCookie = con.getHeaderField("set-cookie");//一定要放在write body之后否则写不了
if (tmpCookie != null)
{
cookieClass.writeRecord(tmpCookie);
cookieClass.setCookie(tmpCookie);
}
in = new DataInputStream(con.openInputStream());
int ch;
while ((ch = in.read()) != -1) {
result = result + (char) ch;
}
overTime = new Date();
time =(int) (overTime.getTime()-startTime.getTime()); //算延时
}catch(IOException ex){
ex.printStackTrace();
}finally {
if (con != null) con.close();
if (in != null) in.close();
if (out != null) out.close();
}
try{
new ProcessWML(result,display,time,cookieClass,hc).process();
}catch(XmlPullParserException xex){
xex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -