在网上找了一个程序段,就是想实现向一个网站发送信息的程序,比如登陆或者查询等。我改了一个程序,但是用不了啊,比如向 www.baidu.com 发送一个关键字 one , 然后返回查询的结果,但是出现了错误
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
public class TestPost {
public static void testPost() throws IOException {
URL url = new URL("http://www.baidu.com");
URLConnection connection = url.openConnection(); connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection
.getOutputStream(), "8859_1");
// 向百度发送的关键字 one
out.write("wd=one");
// remember to clean up
out.flush();
out.close();
// 百度的反应
String sCurrentLine;
String sTotalString;
sCurrentLine = "";
sTotalString = "";
// 这行出错了
InputStream l_urlStream;
l_urlStream = connection.getInputStream();
// 传说中的三层包装阿!
BufferedReader l_reader = new BufferedReader(new InputStreamReader(
l_urlStream));
while ((sCurrentLine = l_reader.readLine()) != null) {
sTotalString += sCurrentLine + "\r\n";
}
System.out.println(sTotalString);
}
public static void main(String[] args) throws IOException {
testPost();
}
}
报错的内容
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at k.TestPost.testPost(TestPost.java:52)
at k.TestPost.main(TestPost.java:65)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
public class TestPost {
public static void testPost() throws IOException {
URL url = new URL("http://www.baidu.com");
URLConnection connection = url.openConnection(); connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection
.getOutputStream(), "8859_1");
// 向百度发送的关键字 one
out.write("wd=one");
// remember to clean up
out.flush();
out.close();
// 百度的反应
String sCurrentLine;
String sTotalString;
sCurrentLine = "";
sTotalString = "";
// 这行出错了
InputStream l_urlStream;
l_urlStream = connection.getInputStream();
// 传说中的三层包装阿!
BufferedReader l_reader = new BufferedReader(new InputStreamReader(
l_urlStream));
while ((sCurrentLine = l_reader.readLine()) != null) {
sTotalString += sCurrentLine + "\r\n";
}
System.out.println(sTotalString);
}
public static void main(String[] args) throws IOException {
testPost();
}
}
报错的内容
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at k.TestPost.testPost(TestPost.java:52)
at k.TestPost.main(TestPost.java:65)
解决方案 »
- 谁碰到过servlet文件下载,调用一次会执行两次,第二次才正式下载
- jsp添加项目中文乱码问题!求助!
- 在SSH项目中 加入Dwr
- 这种下拉列表框Struts框架怎么得到值啊?
- 关于SPRING的配置问题,想知道如果使用了JAVABEAN文件操作SPRING封装的JDBC,SPRING应该怎么配置这个文件啊,急啊!!!
- 关于java中操作Excel文件:(性能最好,开源项目是那个?);顶者有分
- 如何用IntelliJ IDEA部署web项目到tomcat?
- 急,log4j找不到,报错org/apache/log4j/logging找不到
- JSON字符串怎么转List和map集合啊
- 代码出现错误,求助
- struts1.x的url-pattern问题
- 求ssh做的项目
<form name="f" action="s">
<input type="text" name="wd" id="kw" maxlength="100"><input type="submit" value="百度一下" id="su">
<span id="hp">
<a href="/gaoji/preferences.html">设置</a>
<br><a href="/gaoji/advanced.html">高级</a>
</span>
</form>
连接被重置 会不会是www.baidu.com对head进行了判断 补上游览器发送时的head试试
还有
out.flush();
out.close();
这个放到后面关闭 发送"/r/n/r/n"就说明结束
out.write("wd=one/r/n/r/n"); 写成这个样子了,还是不行,这个和系统的登录是不是差不多呢?系统登陆就是发送 userid 和 password 吧,但是这个如果不知道服务器端的后台程序,怎么办呢?
URL url = new URL("http://www.baidu.com/s?wd=aa");
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
final byte[] b = new byte[4096];
int len = 0;
while ((len = is.read(b)) > 0) {
bout.write(b, 0, len);
}
is.close();
System.out.println(bout.toString());
} catch (IOException e) {
e.printStackTrace();
}
请问,Apache 的 httpclient 在哪个包下面呢?我的机器上有个commons-httpclient-2.0alpha1-20020829.jar,不过好像用不了。另外,你说的过滤指的是什么?能有资料让我看看么?
呵呵给个链接给lz参考吧http://hc.apache.org/httpcomponents-client/index.html至于“过滤”,我知道比如淘宝就有为了防止爬虫乱爬,
对某个ip某个时间段的访问次数是有限制的。
都说了是“估计”了,就是没谱,瞎猜,不负责任的了。^^闪砖跑路good luck
所谓一般化服务,就是“用户用浏览器直接访问baidu域名,然后baidu返回内容”这样的服务。
那么肯定只能得到baidu服务器“一般化”的返回内容。
也就是一个html内容。因为返回回来的html内容相对固定(格式、位置、修饰等等)。
一般解析的方式是用正则表达式,解析返回的html里面的内容,提取自己关心的文字。也就是匹配例如"<a>"这样的标签来解析<a></a>里面的内容和href内容。具体的lz就自己找一下或者等楼下的朋友帮忙吧good luck
其实一般的字符串比较也可以完成。
比如定位<a和</a>的位置,然后取得中间的文字,
就是比较麻烦。另外,有些网站提供数据api,可以提交指定的url和参数格式,
然后会返回给你特定的数据格式(大部分是xml的)
也就是相当于一个web service了。
提供这种api的网站有很多,比如那些交友网络社区网站,
靠开放api来提供数据给别的网站。
google的一些内容提供服务也是通过这样开放api来提供的
baidu的搜索有没有这种api需要调查一下,我不知道。
以前google提供过类似这样的api,提交关键词,返回搜索结果。
但是不知道是因为访问量过大还是怎的,最后关闭了。多罗嗦几句,题外话了,呵呵good luck
public static void main(String[] args) throws IOException
{
HttpClient client = new HttpClient();
PostMethod method = getPostMethod();
client.executeMethod(method);
//打印服务器返回的状态
System.out.println(method.getStatusLine());
//打印返回的信息
System.out.println(method.getResponseBodyAsString());
//释放连接
method.releaseConnection();
}
private static PostMethod getPostMethod(){
PostMethod post = new PostMethod("http://localhost:8080/resourceService/faces/Login.jsp");
NameValuePair userName = new NameValuePair("form1:txtUserName","anna");
NameValuePair passWord = new NameValuePair("form1:txtPassword","anna");
post.setRequestBody(new NameValuePair[] { userName, passWord });
return post;
}}
Login.jsp 如下:<?xml version="1.0" encoding="UTF-8"?>
<jsp:root version="1.2"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:ui="http://www.sun.com/web/ui">
<jsp:directive.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"/> <f:view>
<ui:page binding="#{Login.page}" id="page1">
<ui:html binding="#{Login.html}" id="html1">
<ui:head binding="#{Login.head}" id="head1"
title="Welcome to YAWL 2.0: Please Login">
<ui:link binding="#{Login.link}" id="link1"
url="/resources/stylesheet.css"/>
<ui:link binding="#{ApplicationBean.favIcon}" id="lnkFavIcon"
rel="shortcut icon"
type="image/x-icon" url="/resources/favicon.ico"/>
</ui:head> <ui:body binding="#{Login.body}" id="body1"
focus="form1:txtUserName"
style="-rave-layout: grid">
<br/>
<ui:form binding="#{Login.form}" id="form1"> <!-- include banner -->
<div><jsp:directive.include file="pfHeader.jspf"/></div> <center> <ui:panelLayout binding="#{Login.pnlContainer}"
id="pnlContainer"
styleClass="loginPanel"> <ui:label binding="#{Login.lblUserName}"
for="txtUserName"
id="lblUserName"
style="top: 25px"
styleClass="loginLabel"
text="User Name:"/> <ui:textField binding="#{Login.txtUserName}"
id="txtUserName"
style="top: 22px"
styleClass="loginField"/> <ui:label binding="#{Login.lblPassword}"
for="txtPassword"
style="top: 55px"
styleClass="loginLabel"
id="lblPassword"
text="Password:"/> <ui:passwordField binding="#{Login.txtPassword}"
id="txtPassword"
style="top: 52px"
styleClass="loginField"/> <ui:button action="#{Login.btnLogin_action}"
binding="#{Login.btnLogin}"
id="btnLogin"
primary="true"
styleClass="loginButton"
text="Login"/> </ui:panelLayout> <ui:panelLayout binding="#{SessionBean.messagePanel}"
id="msgPanel"
panelLayout="flow"/>
</center>
</ui:form>
</ui:body>
</ui:html>
</ui:page>
</f:view>
</jsp:root>