如何成为Android开发高手
2010-08-31 00:43:00 来源:WEB开发网254
255//String params = "method=save&name="+ URLEncoder.encode("老毕", "UTF-8")+ "&age=28&";//需要发送的参数
256
257StringBuilder parambuilder = new StringBuilder("");
258
259if(params!=null && !params.isEmpty()){
260
261for(Map.Entry
262
263parambuilder.append(entry.getKey()).append("=")
264
265.append(URLEncoder.encode(entry.getValue(), encode)).append("&");
266
267}
268
269parambuilder.deleteCharAt(parambuilder.length()-1);
270
271}
272
273byte[] data = parambuilder.toString().getBytes();
274
275URL url = new URL(path);
276
277HttpURLConnection conn = (HttpURLConnection)url.openConnection();
278
279conn.setDoOutput(true);//允许对外发送请求参数
280
281conn.setUseCaches(false);//不进行缓存
282
283conn.setConnectTimeout(5 * 1000);
284
285conn.setRequestMethod("POST");
286
287//下面设置http请求头
288
289conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
290
291conn.setRequestProperty("Accept-Language", "zh-CN");
292
293conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
更多精彩
赞助商链接