当前页面: 开发资料首页 → J2SE 专题 → 关于HttpURLConnection接收大数据问题
关于HttpURLConnection接收大数据问题
摘要: 关于HttpURLConnection接收大数据问题
我用HttpURLConnection接收数据 只接收一部分数据 怎么办
URL url = new URL(-#34;网址-#34;);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
//设置允许output
huc.setDoOutput(true);
//设置为post方式
huc.setRequestMethod(-#34;POST-#34;);
StringBuffer sb = new StringBuffer();
sb.append(-#34;userName=-#34;+userNme);
sb.append(-#34;-#38;password=-#34;+password);
//post信息
OutputStream os = huc.getOutputStream();
os.write(sb.toString().getBytes(-#34;GBK-#34;));
os.close();
BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream()))
huc.connect();
String line = br.readLine();
while(line != null){
System.out.printli(line);
line = br.readLine();
}
是不是缓冲读取,最后没有flush() and close()啊??
还有把post改成get试试
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URLConnection;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class WebPageReader {
private static URLConnection connection;
private static void connect( String urlString ) {
try {
URL url = new URL(urlString);
connection = url.openConnection();
System.out.println(connection.getClass());
} catch (MalformedURLException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void readContents() {
BufferedReader in = null;
try {
in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String inputLine;
while (
(inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println(-#34;usage: java WebPageReader -#34;
+ -#34;-#60;url-#62;-#34;);
System.exit(0);
}
connect(args[0]);
readContents();
}
}
调用flush()