2022新浪行情数据接口JAVA程序

import java.io.BufferedReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.net.URL;

import java.util.List;

import org.apache.http.HttpEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.utils.URIBuilder;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicHeader;

import org.apache.http.util.EntityUtils;

import sun.net.www.URLConnection;

public class test {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

test reht=new test();

String logPath = "/tmp/test/" + reht.getTime().substring(0, 6)+ ".log";

// reht.writeLog(logPath, "sadfsa");

reht.sendPost("http://hq.sinajs.cn/list=sh601500");

try{

reht.sendGet("http://hq.sinajs.cn/list=sz000685");


reht.sendGet2("http://hq.sinajs.cn/list=RU0");

// reht.sendGet("http://127.0.0.1:8080/testhtp/login1.jsp");

}catch (Exception e) {

System.out.println("发送 get 请求出现异常!" + e);

e.printStackTrace();

}

}

public String sendGet22(String htmlurl) { //请求某个网址,反回内容

CloseableHttpClient client = null;

CloseableHttpResponse response = null;

try{

// 创建HttpClient对象

client = HttpClients.createDefault();

URIBuilder uriBuilder = new URIBuilder(htmlurl);

System.out.println(htmlurl);

HttpGet httpGet = new HttpGet(uriBuilder.build());

httpGet.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"));

httpGet.setHeader(new BasicHeader("Accept", "text/plain;charset=utf-8"));

httpGet.setHeader(new BasicHeader("referer", "http://finance.sina.com.cn"));

response = client.execute(httpGet);

int statusCode = response.getStatusLine().getStatusCode();

System.out.println("statusCode="+statusCode);

if (200== statusCode){

HttpEntity entity = response.getEntity();

String result = EntityUtils.toString(entity,"UTF-8");

System.out.println(result);

response.close();

client.close();

return result;

}else{

System.out.println("HttpClientService-line: {}, errorMsg{}, 97,GET请求失败!");

}

}catch (Exception e){

System.out.println("HttpClientService-line: {}, Exception: {}, 100,+"+ e);

} finally {}

return "";

}

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章