tidus2005 发表于 2013-1-15 03:10:01

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; }} 
页: [1]
查看完整版本: JAVA发送向指定URL发送HTTP请求