62,614
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer result = new StringBuffer();
URL httpurl=null;
HttpURLConnection httpConn=null;
try {
httpurl = new URL("http://www.123.com");
httpConn = (HttpURLConnection) httpurl.openConnection();
httpConn.setRequestMethod("POST");
// httpConn.setReadTimeout(5000);
httpConn.setConnectTimeout(30000);
httpConn.setUseCaches(false);
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.connect();
// OutputStreamWriter outw = new OutputStreamWriter(httpConn.getOutputStream(), "utf-8");
// outw.write("keywords=help");
// outw.flush();
// outw.close();
// InputStreamReader re=new InputStreamReader(httpConn.getInputStream(),"utf-8");
/*运行到下面这条语句就不卡主了无法继续运行,如果设置读取时间,当读取时间到时会发生异常,但下次还是会被卡住*/
BufferedReader ins = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "utf-8"));
// BufferedReader ins = new BufferedReader(re);
String lines;
while ((lines = ins.readLine()) != null) {
result.append(lines);
}
System.out.println(result.toString());
ins.close();
httpConn.connect();
} catch (IOException e) {
System.out.println(e);
}
}