程序流程
手机上传文件至 proxy, proxy再转发至CMA工程
1.手机中部分代码如下:
1 private HttpResponse request(Url aUrl
2 , String aReqParam
3 , Hashtable aRequestProperties
4 , byte[] aPicData)
5 {
6 //send Action
7 this.iListener.httpListener(iInstance, HttpRequestListener.K_CREATE_CONNECT, null);
8
9 //send Action
10 this.iCancel = false;
11 this.iListener.httpListener(iInstance, HttpRequestListener.K_BEGIN_CANCEL, null);
12
13 //建立连接
14 if(!OpenConnection(aUrl))
15 {
16 return this.getErrResponse("无法建立连接.");
17 }
18
19 //send Action
20 this.iListener.httpListener(iInstance, HttpRequestListener.K_SENDING_DATA, null);
21
22 InputStream is = null;
23 DataOutputStream dos = null;
24 int resCode = 0;
25 try
26 {
27 if(aReqParam != null)
28 iHc.setRequestMethod("POST");
29 else
30 iHc.setRequestMethod("GET");
31
32 this.iniRequestProperties(aRequestProperties);
33
34 if(aReqParam != null)
35 {
36 dos = iHc.openDataOutputStream();
37 if (aPicData != null)
38 { //上传图片时的连接,以""标志在proxy里分开参数和图片
39 byte[] picData = aPicData;
40 dos.write((aReqParam+"").getBytes());
41
42 //分次发送图片********************
43 if(picData.length > 1000){
44 int totalSize = picData.length;
45 int sendSize = 1000;
46 //发送的次数
47 int count = (totalSize % sendSize) == 0 ? (totalSize / sendSize)
48 : (totalSize / sendSize + 1);
49
50 int len;
51 for (int i = 0; i < count; i++)
52 {
53 len = (totalSize - i*sendSize) > sendSize ? sendSize : (totalSize - i*sendSize);
54 try {
55 dos.write(picData, sendSize*i, len);
56 } catch (IOException e) {
57 e.printStackTrace();
58 }
59 }
60
61 }else{
62 dos.write(picData);
63 }
64 //********************
65 }else{
66 //不上传图片时的连接
67 dos.writeUTF(aReqParam);
68 }
69 }
70
71 //send Action
72 this.iListener.httpListener(iInstance, HttpRequestListener.K_WAITING_RESPONSE, null);
73
74 resCode = iHc.getResponseCode();
75
76 //do cancel
77 if(this.iCancel)
78 return null;
79
80 //get http header
81 int index = 0;
82 this.iResHttpHeader = new Hashtable();
83 while (true)
84 {
85 String key = iHc.getHeaderFieldKey(index++);
86 if(key == null)
87 {
88 break;
89 }
90
91 String field = iHc.getHeaderField(key);
92
93 iResHttpHeader.put(key, field);
94 }
95
96 //send Action
97 this.iListener.httpListener(iInstance, HttpRequestListener.K_END_CANCEL, null);
98
99 if(resCode == 200)
100 {
101 is = iHc.openInputStream();
102
103 //send Action
104 this.iListener.httpListener(iInstance, HttpRequestListener.K_RECEIVING_DATA, null);
105
106 String mimeType = iHc.getHeaderField("Content-Type");
107
108 if(mimeType == null)
109 {
110 mimeType = "";
111 //return this.getErrResponse("invalide mime type:"+mimeType);
112 }
113
114 String contectType = iHc.getHeaderField("Content-Type");
115 if(contectType!=null && contectType.trim().indexOf("vnd.wap.wml")>=0)
116 {
117 try{
118 Thread.sleep(1000);
119 }catch(Exception e){}
120 return request(aUrl, aReqParam, aRequestProperties, aPicData);
121 }
122
123 if(mimeType.trim().indexOf("image/jpeg") != -1
124 || mimeType.trim().indexOf("image/png") != -1)
125 {
126 return this.getByteArrayResponse(is);
127 }
128 else if(mimeType.trim().indexOf("text/x-zhml") != -1
129 || mimeType.trim().indexOf("text/plain") != -1)
130 {
131 return this.getStreamResponse(is);
132 }
133 else
134 {
135 return this.getErrResponse("invalide mime type:"+mimeType);
136 }
137 }
138 else//if(hc.getResponseCode() == 200)
139 {
140 return this.getErrResponse("ResponseCode:"+resCode);
141 }
142 }
143 catch(IllegalArgumentException e)
144 {
145 return this.getErrResponse("IllegalArgumentException");
146 }
147 catch(ConnectionNotFoundException e)
148 {
149 return this.getErrResponse("ConnectionNotFoundException");
150 }
151 catch(IOException e)
152 {
153 if(iConnCount == 1)//表明是第一次失败
154 {
155 HttpResponse res = new HttpResponse();
156 res.iErrStr = "IOException";
157 res.iTwiceConn = true;
158
159 if(this.iDebug)
160 System.out.println("twice!!!");
161 return res;
162 }
163 else
164 {
165 return this.getErrResponse("IOException");
166 }
167 }
168 catch(Exception e)
169 {
170 return this.getErrResponse("Exception:1");
171 }
172 finally
173 {
174 this.destroyOutputStream(dos);
175
176 try
177 {
178 // if(is != null)
179 // {
180 // is.close();
181 // is = null;
182 // }
183 if (iHc != null)
184 {
185 iHc.close();
186 iHc = null;
187 }
188 }
189 catch (Exception e)
190 {
191 return this.getErrResponse("Exception:2");
192 }
193 }
194 }
其中,测试建立连接的方法如下:
1 private HttpConnection iHc = null;
2 private boolean OpenConnection(Url aUrl)
3 {
4 iHc = null;
5
6 try
7 {
8 if(!iIsProxy)//direct connection
9 {
10 String url = aUrl.iProtocol
11 + "://"
12 + aUrl.iHost;
13
14 if(aUrl.iPort != null)
15 url += (":" + aUrl.iPort);
16 if(aUrl.iPath != null)
17 url += ("/" + aUrl.iPath);
18
19 iHc = (HttpConnection) Connector.open(
20 url
21 , Connector.READ_WRITE,true);
22 }
23 else//proxy connection
24 {
25 iHc = (HttpConnection)Connector.open("http://10.0.0.172:80"
26 + "/"
27 + aUrl.iPath
28 , Connector.READ_WRITE, true);
29
30 if(aUrl.iPort != null)
31 {
32 iHc.setRequestProperty("X-Online-Host"
33 , aUrl.iHost + ":" + aUrl.iPort);
34 }
35 else
36 {
37 iHc.setRequestProperty("X-Online-Host"
38 , aUrl.iHost);
39 }
40 }
41
42 if(this.iHc != null)
43 return true;
44 else
45 return false;
46 }
47 catch(Exception e)
48 {
49 return false;
50 }
51 }
附:没有文件上传的方法,如下:
private HttpResponse request(Url aUrl
, String aReqParam
, Hashtable aRequestProperties)
{
//send Action
this.iListener.httpListener(iInstance, HttpRequestListener.K_CREATE_CONNECT, null);
//send Action
this.iCancel = false;
this.iListener.httpListener(iInstance, HttpRequestListener.K_BEGIN_CANCEL, null);
//建立连接
if(!OpenConnection(aUrl))
{
return this.getErrResponse("无法建立连接.");
}
//send Action
this.iListener.httpListener(iInstance, HttpRequestListener.K_SENDING_DATA, null);
InputStream is = null;
DataOutputStream dos = null;
int resCode = 0;
try
{
if(aReqParam != null)
iHc.setRequestMethod("POST");
else
iHc.setRequestMethod("GET");
this.iniRequestProperties(aRequestProperties);
if(aReqParam != null)
{
dos = iHc.openDataOutputStream();
dos.writeUTF(aReqParam);
}
//send Action
this.iListener.httpListener(iInstance, HttpRequestListener.K_WAITING_RESPONSE, null);
resCode = iHc.getResponseCode();
//do cancel
if(this.iCancel)
return null;
//get http header
int index = 0;
this.iResHttpHeader = new Hashtable();
while (true)
{
String key = iHc.getHeaderFieldKey(index++);
if(key == null)
{
break;
}
String field = iHc.getHeaderField(key);
iResHttpHeader.put(key, field);
}
//send Action
this.iListener.httpListener(iInstance, HttpRequestListener.K_END_CANCEL, null);
if(resCode == 200)
{……
……