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

向某个指定的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;
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值