String sTmp="http://162.168.0.2/query.jsp";
try{  
 URL url=new URL(sTmp);      HttpURLConnection connect =(HttpURLConnection) url.openConnection();
 connect.setDoInput(true);
connect.setDoOutput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream())); String line = null;
 StringBuffer content= new StringBuffer();while((line = in.readLine()) != null){//line为返回值,这就可以判断是否成功、content.append(line);
}in.close() ;
in=null;
url=null;
String msg = content.toString();
System.err.println(msg);
             
}catch(IOException ex){
ex.printStackTrace();
}catch(Exception e){System.out.println("错误:");
System.out.println(e.getStackTrace());}}

解决方案 »

  1.   

    多谢老大
    有个东西忘了说了向http://162.168.0.2/query.jsp发送post时需要提交一个表单数据
    比如说表单里面有id和psw两个字段是不是要在
    HttpURLConnection connect =(HttpURLConnection) url.openConnection();
    前面写点什么东东啊??
      

  2.   

    String sTmp="http://162.168.0.2/query.jsp?id=xxx&psw=xxx";
      

  3.   

    这样传过去的是get数据,query.jsp好象调用的是一个只实现了doPost方法的servlet,只处理post提交的表单数据
    用http://162.168.0.2/query.jsp?id=xxx&psw=xxx访问得到的是一个错误页面
    只能用post提交表单
      

  4.   

    try {
    URL url = new URL("http://162.168.0.2/query.jsp");
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);
    //发送域信息
    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "8859_1");
    out.write("username=test&password=test");//这里组织域信息
    out.flush();
    out.close();
    //获取返回数据
    InputStream in=connection.getInputStream();
    .......
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      

  5.   

    表单里面有id和psw两个字段
    =======================
    把上面的username,password换成你想要的就可以了!这段代码做了类似如下的工作
    POST http://162.168.0.2/query.jsp HTTP 1.1
    ACCEPT: text/plain
    Content-type: application/x-www-form-urlencoded
    Content-length: xxxx
    username=test
    password=test