首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

Android_HttpClient_get请求post表单交付上传

2013-10-01 
Android_HttpClient_get请求post表单提交上传本博文为子墨原创,转载请注明出处!http://blog.csdn.net/zimo

Android_HttpClient_get请求post表单提交上传
本博文为子墨原创,转载请注明出处!http://blog.csdn.net/zimo2013/article/details/12193427关于HttpUrlConnection用法1.HttpRequestBase(HttpGet..)

HttpClient 支持多种访问网络的方式,包括GET, HEAD, POST, PUT, DELETE, TRACE and OPTIONS. 其对应子类为HttpGet, HttpHead, HttpPost, HttpPut, HttpDelete, HttpTrace, and HttpOptions.但在使用时多为HttpGet, HttpPost两种方式。

/** * 通过post完成文件的上传 */private static void postFile() {HttpClient httpClient = new DefaultHttpClient();HttpPost httpPost = new HttpPost("http://127.0.0.1:8080/My/upload");try {// 需要上传的文件String root = "D:/api/";String fileName = "JDK6.0 中文文档.CHM";File uploadFile = new File(root+fileName);//定义FileEntity对象HttpEntity entity = new FileEntity(uploadFile);//为httpPost设置头信息httpPost.setHeader("filename", URLEncoder.encode(fileName,"utf-8"));//服务器可以读取到该文件名httpPost.setHeader("Content-Length", String.valueOf(entity.getContentLength()));//设置传输长度httpPost.setEntity(entity);//设置实体对象// httpClient执行httpPost提交HttpResponse response = httpClient.execute(httpPost);// 得到服务器响应实体对象HttpEntity responseEntity = response.getEntity();if (responseEntity != null) {System.out.println(EntityUtils.toString(responseEntity, "utf-8"));System.out.println("文件 "+fileName+"上传成功!");} else {System.out.println("服务器无响应!");}} catch (Exception e) {e.printStackTrace();} finally {// 释放资源httpClient.getConnectionManager().shutdown();}}

热点排行