六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 43|回复: 0

JAVA发送向指定URL发送HTTP请求

[复制链接]

升级  50.67%

34

主题

34

主题

34

主题

秀才

Rank: 2

积分
126
 楼主| 发表于 2013-1-15 03:10:01 | 显示全部楼层 |阅读模式
向某个指定的URL发送request请求package com.moresee.http;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;public class SendURLPost { private String urlStr; private URL url; private HttpURLConnection httpURLConnection; private String response; public static void main(String[] args) throws IOException {  new SendURLPost("708"); } public SendURLPost(String articleId) throws IOException {    urlStr = "http://www.bdqncn.com/readnews.asp";  url = new URL(urlStr);  httpURLConnection = (HttpURLConnection) url.openConnection(); //获取连接  httpURLConnection.setRequestMethod("POST"); //设置请求方法为POST, 也可以为GET  httpURLConnection.setDoOutput(true);    StringBuffer param = new StringBuffer("ArticleId=");  //请求URL的查询参数  param.append(articleId);  OutputStream os = httpURLConnection.getOutputStream();  os.write(param.toString().getBytes());  os.flush();  os.close();    InputStream is = httpURLConnection.getInputStream();  BufferedReader br = new BufferedReader(new InputStreamReader(is));  StringBuilder sb = new StringBuilder();  while (br.read() != -1) {   sb.append(br.readLine());  }  String content = new String(sb);  content = new String(content.getBytes("GB2312"), "ISO-8859-1");  System.out.println(content);  br.close(); } public String getUrlStr() {  return urlStr; } public void setUrlStr(String urlStr) {  this.urlStr = urlStr; } public URL getUrl() {  return url; } public void setUrl(URL url) {  this.url = url; } public HttpURLConnection getHttpURLConnection() {  return httpURLConnection; } public void setHttpURLConnection(HttpURLConnection httpURLConnection) {  this.httpURLConnection = httpURLConnection; } public String getResponse() {  return response; } public void setResponse(String response) {  this.response = response; }} 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表