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());}}
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());}}
解决方案 »
- 如何在javabean中往jsp的application内置对象中写入值?不使用struts2,纯javabean怎么用?不知什么函数获得application
- 如何实现及时语。。。
- 注册页面与数据库
- 请各位高手帮忙看看
- 用jsp接收flex转入数据,生成excel
- 在struts中,全局转发和本地转发有和区别,在Action中 execute()和perform()的区别??
- 类百度搜索热词功能,遇到的问题... 求大神解答
- 请大虾比较tomcat,resin,jrun的优劣
- 模态窗口提交的问题
- 一个简单问题
- jboss4.0下如何单独运行集成的tomcat
- 欢迎加入java技术讨论群 (2906022)
有个东西忘了说了向http://162.168.0.2/query.jsp发送post时需要提交一个表单数据
比如说表单里面有id和psw两个字段是不是要在
HttpURLConnection connect =(HttpURLConnection) url.openConnection();
前面写点什么东东啊??
用http://162.168.0.2/query.jsp?id=xxx&psw=xxx访问得到的是一个错误页面
只能用post提交表单
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();
}
=======================
把上面的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