1 //HttpGet 请求包 2 HttpGet httpGet = new HttpGet(sbUrl.tostring) 3 //获取请求行对象 4 RequestLine requestLine = httpGet.getRequestLine(); 5 System.out.println("RequestLine:" + requestLine.toString()); 6 7 HttpClient httpClient = new DefaultHttpClient();//构造一个默认的 HttpClient 客户端对象 8 HttpResponse httpResponse = null; //定义一个 HttpResponse 响应对象 9 HttpEntity httpEntity = null; //定义一个 HttpEntity 响应内容实体 10 InputStream is = null; 11 12 try 13 { 14 //发送一个请求对象,并返回该请求对象的 HttpResponse 响应对象 15 httpResponse = httpClient.execute(httpGet); 16 //请求成功,httpResponse.getStatusLine() 获取响应状态行对象,getStatusCode() 获取状态码 17 if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 18 { 19 httpEntity = httpResponse.getEntity(); //获取 HttpEntity 响应实体 20 printResponse_AllHeader(httpResponse); //打印 Response 中所有的 Header 21 printContentEncodingHeader(httpRespEntity); //打印 Response 内容编码 Header 22 printContentType_Header(httpRespEntity); //打印 HttpResponse 内容类型 Header 23 24 // //获取实体内容方法一 25 // is = httpEntity.getContent(); 26 // int iChar; 27 // StringBuffer sbContent = new StringBuffer(); 28 // while((iChar = is.read()) != -1) 29 // { 30 // sbContent.append((char)iChar); 31 // } 32 // //获取到的内容转换成 GBK 编码 33 // String strContent = new String(sbContent.toString().getBytes("iso_8859_1"), "GBK"); 34 35 //获取实体内容方法二 36 String strContent = EntityUtils.toString(httpEntity); 37 tvContent.setText(strContent); 38 } 39 40 } 41 catch (ClientProtocolException e) 42 { 43 e.printStackTrace(); 44 } 45 catch (IOException e) 46 { 47 e.printStackTrace(); 48 } 49 finally 50 { 51 if(is != null) 52 { 53 try 54 { 55 is.close(); 56 is = null; 57 } 58 catch (IOException e) 59 { 60 e.printStackTrace(); 61 } 62 } 63 } 64 65 /** 66 * 函数名称 : printResponse_AllHeader 67 * 功能描述 : 打印 Response 中所有的 Header 68 * 参数及返回值说明: 69 * @param httpResponse 70 * 71 * 修改记录: 72 * 日期 :2012-7-5 上午11:06:30 修改人:gy 73 * 描述 : 74 * 75 */ 76 private void printResponse_AllHeader(HttpResponse httpResponse) 77 { 78 //获取响应中所有的 Header 79 Header[] arrHeader = httpResponse.getAllHeaders(); 80 System.out.println("=====所有的 Header begin====="); 81 for(int i = 0; i < arrHeader.length; i++) 82 { 83 Header hearder = arrHeader[i]; 84 System.out.println("hearder.getName() = " + hearder.getName() 85 + " hearder.getValue() = " + hearder.getValue()); 86 } 87 System.out.println("=====所有的 Header end====="); 88 } 89 90 /** 91 * 函数名称 : printContentEncodingHeader 92 * 功能描述 : 打印 Response 内容编码 Header 93 * 参数及返回值说明: 94 * @param httpEntity 95 * 96 * 修改记录: 97 * 日期 :2012-7-5 上午11:08:15 修改人:gy 98 * 描述 : 99 * 100 */ 101 private void printContentEncodingHeader(HttpEntity httpEntity) 102 { 103 System.out.println("=====“内容编码”的 Header begin====="); 104 //获取“内容编码”的 Header 105 Header contentEncode_Header = httpEntity.getContentEncoding(); 106 if(contentEncode_Header != null) 107 { 108 System.out.println("contentEncode_Header.getName() = " + contentEncode_Header.getName() 109 + " contentEncode_Header.getValue() = " + contentEncode_Header.getValue()); 110 111 } 112 System.out.println("=====“内容编码”的 Header end====="); 113 } 114 115 116 /** 117 * 函数名称 : printContentType_Header 118 * 功能描述 : 打印 HttpResponse 内容类型 Header 119 * 参数及返回值说明: 120 * @param httpEntity 121 * 122 * 修改记录: 123 * 日期 :2012-7-5 上午11:00:36 修改人:gy 124 * 描述 : 125 * 126 */ 127 private void printContentType_Header(HttpEntity httpEntity) 128 { 129 // Content-Type: text/html; charset=UTF-8 130 System.out.println("=====“内容类型”的 Header begin====="); 131 //获取“内容类型”的 Header 132 Header contentType_Header = httpEntity.getContentType(); 133 if(contentType_Header != null) 134 { 135 System.out.println("contentType_Header.getName() = " + contentType_Header.getName() 136 + " contentType_Header.getValue() = " + contentType_Header.getValue()); 137 138 } 139 System.out.println("=====“内容类型”的 Header end====="); 140 } 141 142 //HttpPost 请求 143 //设置请求的参数 144 List<NameValuePair> list = new ArrayList<NameValuePair>(); 145 list.add(new BasicNameValuePair("textAccountNumber", account)); 146 list.add(new BasicNameValuePair("textPwd", pwd)); 147 148 try 149 { 150 //创建请求实体,将参数放到这个实体中,设置请求的字符集 151 HttpEntity httpEntity = new UrlEncodedFormEntity(list, "gbk"); 152 HttpPost = httpPost = new HttpPost(sbUrl.tostring); 153 //android 客户端发送到 Java 服务器时,中文数据要这样处理 154 httpPost.addHeader("name", URLEncoder.encode("中文", HTTP.UTF_8)); 155 156 //获取请求行对象 157 RequestLine requestLine = httpPost.getRequestLine(); 158 System.out.println("RequestLine:" + requestLine.toString()); 159 160 //设置 HttpPost 请求实体(把包体设置到这个 POST 请求包中) 161 httpPost.setEntity(httpReqEntity); 162 HttpResponse httpResponse = httpClient.execute(httpPost); 163 164 //请求成功 165 if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 166 { 167 //获取 HttpEntity 响应实体 168 HttpEntity httpRespEntity = httpResponse.getEntity(); 169 //获取实体内容方法 170 String strContent = EntityUtils.toString(httpRespEntity, "GBK"); 171 172 //获取包体内容的字符集 173 String strContentCharSet = EntityUtils.getContentCharSet(httpRespEntity); 174 System.out.println("strContentCharSet:" + strContentCharSet); 175 } 176 177 } 178 catch (UnsupportedEncodingException e) 179 { 180 e.printStackTrace(); 181 } 182 catch (ClientProtocolException e) 183 { 184 e.printStackTrace(); 185 } 186 catch (IOException e) 187 { 188 e.printStackTrace(); 189 } 190 finally 191 { 192 if(is != null) 193 { 194 try 195 { 196 is.close(); 197 is = null; 198 } 199 catch (IOException e) 200 { 201 e.printStackTrace(); 202 } 203 } 204 } 205 }