Android HTTP请求 POST、GET
//code by:博客园-曹永思
Android HTTP请求 POST、GET
//HTTP 请求 GET方式 public String GetHttpGet() { String result = null; URL url = null; HttpURLConnection connection = null; InputStreamReader in = null; try { url = new URL("http://192.168.1.202:803/t.ashx?id=sdaf"); connection = (HttpURLConnection) url.openConnection(); in = new InputStreamReader(connection.getInputStream()); BufferedReader bufferedReader = new BufferedReader(in); StringBuffer strBuffer = new StringBuffer(); String line = null; while ((line = bufferedReader.readLine()) != null) { strBuffer.append(line); } result = strBuffer.toString(); } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; }
//HTTP 请求 POST方式 private String GetHttpPost() { String r = ""; try { final String SERVER_URL = "http://192.168.1.202:803/t.ashx"; // 定义需要获取的内容来源地址 HttpPost request = new HttpPost(SERVER_URL); // 根据内容来源地址创建一个Http请求 List params = new ArrayList(); params.add(new BasicNameValuePair("id", "London")); // 添加必须的参数 request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // 设置参数的编码 HttpResponse httpResponse = new DefaultHttpClient() .execute(request); // 发送请求并获取反馈 // 解析返回的内容 if (httpResponse.getStatusLine().getStatusCode() != 404) { String result = EntityUtils.toString(httpResponse.getEntity()); // System.out.println(result); r = result; } } catch (Exception e) { } return r; } .net重写URL:http://www.cnblogs.com/yonsy/archive/2012/09/21/2696935.html
欢迎转载,转载请注明出处,希望帮到更多人。