您的位置:首页 > 编程语言 > Java开发

java抓取有验证的页面内容

2016-01-26 17:57 447 查看
package zz.test.ssm.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/mq")
public class MqQueueController {

@RequestMapping(value = "queue", method = RequestMethod.GET)
@ResponseBody
public String getMqQueue(HttpServletRequest request) throws HttpException, IOException {
String response = "";
String queueName = request.getParameter("queue");
String username = "user";
String password = "pass";
String url = "http://192.168.50.2:1002/api/queues/%2Femsfeedback/"+queueName;

HttpClient client = new HttpClient();
client.getState().setCredentials(
new AuthScope("192.168.50.2", 1002),
new UsernamePasswordCredentials(username, password));
GetMethod get = new GetMethod( url );
get.setDoAuthentication( true );
int status = client.executeMethod( get );
response = get.getResponseBodyAsString();
System.out.println(status+ "\n" + get.getResponseBodyAsString());
get.releaseConnection();
return response;
}
}


参考:

http://www.blogjava.net/jelver/articles/162339.html

/article/5031076.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: