采用HttpURLConnection访问服务端
2013-06-17 20:54
483 查看
How to use HttpURLConnection POST data to web server?
Here is one sample.... public static String excutePost(String targetURL, String urlParameters) { URL url; HttpURLConnection connection = null; try { //Create connection url = new URL(targetURL); connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length)); connection.setRequestProperty("Content-Language", "en-US"); connection.setUseCaches (false); connection.setDoInput(true); connection.setDoOutput(true); //Send request DataOutputStream wr = new DataOutputStream ( connection.getOutputStream ()); wr.writeBytes (urlParameters); wr.flush (); wr.close (); //Get Response InputStream is = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; StringBuffer response = new StringBuffer(); while((line = rd.readLine()) != null) { response.append(line); response.append('\r'); } rd.close(); return response.toString(); } catch (Exception e) { e.printStackTrace(); return null; } finally { if(connection != null) { connection.disconnect(); } } } ...
The urlParameters is a URL encoded string.
String urlParameters = "fName=" + URLEncoder.encode("???", "UTF-8") + "&lName=" + URLEncoder.encode("???", "UTF-8")
相关文章推荐
- Android访问网络,使用HttpURLConnection还是HttpClient?
- HttpURLConnection访问网络
- Android之使用HttpURLConnection进行网络访问
- 两个java项目的服务端通信之HttpURLConnection方式
- 用HttpURLConnection即时访问网络图片的小Demo
- 使用HttpURLConnection采用get方式请求数据-----乱码问题
- Java 通过HttpURLConnection Post方式提交xml,并从服务端返回数据
- Android访问网络,使用HttpURLConnection还是HttpClient?
- 使用HttpURLConnection访问web页面
- Android开发中,HttpURLConnection与HttpClient哪个用来访问网络更好?
- 使用Http协议访问网络—使用HttpURLConnection
- HttpUrlConnecion访问Http服务端的步骤
- 使用HttpURLConnection和使用OkHttp来进行网络访问
- Android-网络访问-HttpURLConnection用法详解
- 06_android入门_采用HttpURLConnection的POST方式实现登陆案例
- 采用HttpURLConnection的GET方式实现登陆案例
- 网络访问 URL,HttpURLConnection及 HttpClient:
- 使用 HTTP 协议访问网络的两种方式:HttpURLConnection 和 HttpClient
- HTTP访问的两种方式(HttpClient和HttpURLConnection)
- 简单使用URLConnection、HttpURLConnection和HttpClient访问网络资源