采用HttpURLConnection访问服务端
来源:互联网 发布:linux常用网络命令大全 编辑:程序博客网 时间:2024/05/19 02:23
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")
- 采用HttpURLConnection访问服务端
- HttpURLConnection 服务端后台demo
- HttpURLConnection(访问网络)
- 使用HttpURLConnection访问网络
- HttpURLConnection访问网络
- 使用HttpURLConnection访问网络
- android HttpURLConnection网络访问
- 使用HttpURLConnection访问网络
- HttpURLConnection访问百度
- 用HttpURLConnection访问网络
- 网络访问之HttpURLConnection
- 使用HttpURLConnection访问网络
- 网络访问之HttpURLConnection
- 使用HttpURLConnection采用Post方式请求数据
- 使用HttpURLConnection采用Post方式请求数据
- HttpURLConnection POST 服务端无法读取数据
- httpurlconnection发送文件到服务端并接收
- httpurlconnection发送文件到服务端并接收
- 一个简单地音乐播放程序
- Dijkstra算法
- java版UC加密
- POJ 1011 Sticks
- Archlinux to Debian小记
- 采用HttpURLConnection访问服务端
- 理解fork()的一次调用两次执行
- BeginInvoke与EndInvoke方法解决多线程接收委托返回值问题
- HDU 寒冰王座
- 图片上传并预览(IE,火狐浏览器兼容问题)
- Request读取中文数据乱码的原因,以及解决办法
- 备忘:Api查询网址
- [转载]C# 写Windows服务程序
- 解决全屏UI中输入法的问题(一)