JAVA模拟发送HTTP请求
2012-08-31 21:41
381 查看
/*
* 得到返回的内容
*/
public static String getResult(String urlStr, String content) {
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();//新建连接实例
connection.setDoOutput(true);//是否打开输出流 true|false
connection.setDoInput(true);//是否打开输入流true|false
connection.setRequestMethod("POST");//提交方法POST|GET
connection.setUseCaches(false);//是否缓存true|false
connection.connect();//打开连接端口
DataOutputStream out = new DataOutputStream(connection.getOutputStream());//打开输出流往对端服务器写数据
out.writeBytes(content);//写数据,也就是提交你的表单 name=xxx&pwd=xxx
out.flush();//刷新
out.close();//关闭输出流
BufferedReader reader = new BufferedReader(new InputStreamReader(connection
.getInputStream(), "utf-8"));//往对端写完数据 对端服务器返回数据 ,以BufferedReader流来读取
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();//关闭连接
}
}
return null;
}
转载自:http://wenwen.soso.com/z/q183356197.htm
* 得到返回的内容
*/
public static String getResult(String urlStr, String content) {
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();//新建连接实例
connection.setDoOutput(true);//是否打开输出流 true|false
connection.setDoInput(true);//是否打开输入流true|false
connection.setRequestMethod("POST");//提交方法POST|GET
connection.setUseCaches(false);//是否缓存true|false
connection.connect();//打开连接端口
DataOutputStream out = new DataOutputStream(connection.getOutputStream());//打开输出流往对端服务器写数据
out.writeBytes(content);//写数据,也就是提交你的表单 name=xxx&pwd=xxx
out.flush();//刷新
out.close();//关闭输出流
BufferedReader reader = new BufferedReader(new InputStreamReader(connection
.getInputStream(), "utf-8"));//往对端写完数据 对端服务器返回数据 ,以BufferedReader流来读取
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();//关闭连接
}
}
return null;
}
转载自:http://wenwen.soso.com/z/q183356197.htm
相关文章推荐
- JAVA模拟发送HTTP请求
- java模拟HTTP请求,发送JSON数据
- Java发送Http请求,模拟表单上传文件
- php利用socket函数模拟发送http请求
- java利用java.net.URLConnection发送HTTP请求的方法详解
- java 常见几种发送http请求案例
- Java后台模拟向Servlet发送POST文件上传请求(转载)
- Java 发送http请求 HttpURLConnection
- Java发送http请求 (get 与 post方法请求)
- java发送http请求
- Java 后台模拟发送 POST GET 请求
- JAVA发送HTTP请求,返回HTTP响应内容,实例及应用
- java发送http请求
- 黑马程序员--Java基础学习之网络编程(TCP、UDP、Socket、模拟发送和接收数据)
- java 发送http请求 返回字符串 再进行解析(略)
- Java模拟新浪和腾讯自动登录并发送微博
- JAVA发送http请求
- 使用Fiddler模拟发送Http请求
- HttpURLConnection简单用法-java模拟HTTP请求
- Java发送http请求 (get 与 post方法请求)转贴