您的位置:首页 > 其它

C66xDSP芯片—semaphore 2 介绍

2013-12-25 20:56 405 查看
有许多的情况我们都需要自己查看一个网页,并且执行这个网页上的某个方法,比如百度的搜索,http://www.baidu.com/s?wd=google&n=2,你可以直接在地址栏中输入,也可以通过Java程序来执行这个过程:
写个示例:
public static void main(String[] args) throws Exception{
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(new HttpHost(new URI("")));//主机设置,写的是http://www.baidu.com
HttpMethod method = null;
byte[] responseBody = null;
try {
method = getPostMethod("");//设置请求URL,可以是http://www.baidu.com

client.executeMethod(method);
responseBody = method.getResponseBody();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
method.releaseConnection();
}
System.out.println(new String(responseBody,"GBK"));
}
private static HttpMethod getPostMethod(String url){
PostMethod post = new PostMethod("/s");
NameValuePair[] values = {new NameValuePair("wd","google"),new NameValuePair("n","2")};//设置请求参数
post.setRequestBody(values);
//都是通过key和value的关系设置值
post.setRequestHeader("Host", "");
post.setRequestHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E)");
post.setRequestHeader("Accept","image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
post.setRequestHeader("Accept-Language", "zh-CN");
post.setRequestHeader("Accept-Encoding", "gzip,deflate");
post.setRequestHeader("Referer","");
post.setRequestHeader("Cookie", "");
post.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

return post;
}
这个过程主要的是设置请求的参数,比如说请求头和请求体,并且通过client.executeMethod的方法来执行,并且返回服务器响应的信息,可以从中提取你所需要的信息。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: