url直接拼接地址会超过限制,所以在开发中我们一般是将请求的参数写到body体中,下面我使用的是android的http网络框架,在实现的过程中出现了很多问题 ,现在代码记录:
final byte[] zipSendDateService = zipSendDate(SelectSendDate().getBytes()); //上送的字符
content = httpClient.getUploadSendData();//请求的报文
//http的post请求,调用下面的post请求
httpClient.post(context, content, zipSendDateService,
"application/octet-stream",new AsyncHttpResponseHandler() {
@Override//请求成功
public void onSuccess(int statusCode,Header[] headers, byte[] responseBody) {
processSendUploadResult(new String(responseBody));}
@Override //请求失败
public void onFailure(int statusCode,Header[] headers, byte[] responseBody,
Throwable error) {
}
});
post的方式将字符串写到body体中请求服务器
//获得上面方法的参数
public RequestHandle post(Context context, String content,
byte[] zipSendDateService, String contentType,
ResponseHandlerInterface responseHandler) {
//字节转成字节输入流
ByteArrayInputStream bis = new ByteArrayInputStream(zipSendDateService);
将流写到Entity中
InputStreamEntity entity = new InputStreamEntity(bis,
zipSendDateService.length);
try {
//转码
content = URLEncoder.encode(content, HTTP.UTF_8);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//获得地址并且拼接json到url后面
String url = getRequestUrl()+"?JSON="
+ content;
//请求服务器
return httpClient.post(context, url, entity, contentType,
responseHandler);
}
服务器可以直接读取body中的字符串