在网上找了一个程序段,就是想实现向一个网站发送信息的程序,比如登陆或者查询等。我改了一个程序,但是用不了啊,比如向 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)

解决方案 »

  1.   

    以前我是直接向百度发“String="http://www.baidu.com/s?wd=aa"”这个字符串的,你可以试一下
      

  2.   

    差不多吧,实际上是想写一个登陆的程序,但是觉得登陆和百度查询差不多吧,所以就问了一个简单的,我把HTML打开看了,找到了其中的form上面要提交的东西,下面是首页上面的一个表单,但是就是自己的程序发送总有异常啊
    <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>
      

  3.   

    Exception in thread "main" java.net.SocketException: Connection reset
    连接被重置 会不会是www.baidu.com对head进行了判断 补上游览器发送时的head试试
    还有
    out.flush();     
    out.close(); 
    这个放到后面关闭 发送"/r/n/r/n"就说明结束
      

  4.   


    out.write("wd=one/r/n/r/n"); 写成这个样子了,还是不行,这个和系统的登录是不是差不多呢?系统登陆就是发送 userid 和 password 吧,但是这个如果不知道服务器端的后台程序,怎么办呢?
      

  5.   

       try {
    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();
      

  6.   

    用apache的httpclient试试呗,估计是baidu那边对什么html头的信息进行过滤了可以先装个浏览器数据拦截软件看看发出去什么,然后模拟一个。good luck
      

  7.   

    TO 心细的依恋: 谢谢,你给的示例,能够运行
    请问,Apache 的 httpclient 在哪个包下面呢?我的机器上有个commons-httpclient-2.0alpha1-20020829.jar,不过好像用不了。另外,你说的过滤指的是什么?能有资料让我看看么?
      

  8.   

    我也没用过,只是听说过比较好用。
    呵呵给个链接给lz参考吧http://hc.apache.org/httpcomponents-client/index.html至于“过滤”,我知道比如淘宝就有为了防止爬虫乱爬,
    对某个ip某个时间段的访问次数是有限制的。
    都说了是“估计”了,就是没谱,瞎猜,不负责任的了。^^闪砖跑路good luck
      

  9.   

    嗯,谢谢各位,再问一个问题,现在感觉写的东西就是向服务器发送一个类似于 GET 的请求,服务器返回的是一个 HTML 文件,我想问的是:是不是服务器返回的只有这一种形式,客户段只能接受 HTML, 然后自己解析。比如,向百度发送的 GET, 返回了一个网页,但实际上,我只关心返回的关键字对应连接,也就是说,怎么才能获得我只关心的内容呢?我想要的是关键对应的连接,但实际上给的是个网页。
      

  10.   

    我又来啦,继续贫两句。因为你想要取得的是“一般化”服务的数据,
    所谓一般化服务,就是“用户用浏览器直接访问baidu域名,然后baidu返回内容”这样的服务。
    那么肯定只能得到baidu服务器“一般化”的返回内容。
    也就是一个html内容。因为返回回来的html内容相对固定(格式、位置、修饰等等)。
    一般解析的方式是用正则表达式,解析返回的html里面的内容,提取自己关心的文字。也就是匹配例如"<a>"这样的标签来解析<a></a>里面的内容和href内容。具体的lz就自己找一下或者等楼下的朋友帮忙吧good luck
      

  11.   

    恩,谢谢。我继续问。实际上,返回什么取决于服务器端的程序是怎么完成,服务器只端返回的内容是 html, 那么我的得到也就是 html,如果我想得到特定的内容,那么我就需要“提炼”它,是吧?如果需要“提炼”,有什么方法么?是用正则表达是么?
      

  12.   

    提炼这个词很到位,呵呵正则表达式是提炼的最...最常用的方法。
    其实一般的字符串比较也可以完成。
    比如定位<a和</a>的位置,然后取得中间的文字,
    就是比较麻烦。另外,有些网站提供数据api,可以提交指定的url和参数格式,
    然后会返回给你特定的数据格式(大部分是xml的)
    也就是相当于一个web service了。
    提供这种api的网站有很多,比如那些交友网络社区网站,
    靠开放api来提供数据给别的网站。
    google的一些内容提供服务也是通过这样开放api来提供的
    baidu的搜索有没有这种api需要调查一下,我不知道。
    以前google提供过类似这样的api,提交关键词,返回搜索结果。
    但是不知道是因为访问量过大还是怎的,最后关闭了。多罗嗦几句,题外话了,呵呵good luck
      

  13.   

    恩,楼上说得很好,再问一个问题,我现在要登录到一个系统上去,这个程序在我的本地,浏览器的url是http://localhost:8080/resourceService/faces/Login.jsp,我想发送用户名和密码,现在有点困惑,到应该怎么做:我不知道该怎么发送,发送什么我写了个程序,不管我发送什么,返回的都是这个登陆路界面html,我的程序如下public class SimpleClient {
    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>