HttpURLConnection请求无结果问题

tongkuang 2009-12-01 04:13:15
请求两个地址,其中第一个可以返回结果,但第二个地址无结果,
但http请求结果都是 url_con.getResponseCode()=200 就是打印不出来请求结果,郁闷中。。。

这是为什么呢?


package readXml;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.*;

public class ParseXML
{
public static void main(String arge[]) throws Exception
{
loadXml("http://wap.sonyericsson.com/UAprof/K700cR201.xml");
loadXml("http://nds1.nds.nokia.com/uaprof/NN78-1r100.xml");
loadXml("http://www.sohu.com");
}

public static void loadXml(String xmlUrl) throws IOException
{
URL url = null;
HttpURLConnection url_con = null;
StringBuffer tempStr =null;
FileWriter fw = null;
// url = new URL(xmlUrl.replaceAll(" ", "%20"));
url = new URL(xmlUrl);
url_con = (HttpURLConnection) url.openConnection();
url_con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
url_con.setConnectTimeout(3000);
url_con.setReadTimeout(3000);
url_con.setRequestMethod("POST");
url_con.setDoOutput(true);
// url_con.getOutputStream().write(null);
url_con.getOutputStream().flush();
url_con.getOutputStream().close();
url_con.getInputStream();

System.out.println("----code:" + url_con.getResponseCode() );


fw = new FileWriter("d:/temp/wap/cc.xml");
InputStream in = url_con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in));
tempStr = new StringBuffer();
String inputLine=null;
System.out.println("---------" + (rd.readLine()));
System.out.println("---------" + (rd.read()));
while ((inputLine = rd.readLine()) != null)
{
if (inputLine.length() > 0)
{
System.out.println("----code-sss------" + inputLine = rd.readLine());
}
System.out.println("----code-------");
tempStr.append(inputLine);
tempStr.append("\n");
}


if (url_con != null)
{
url_con.disconnect();
}
fw.write(tempStr.toString());
fw.close();
rd.close();
int code = url_con.getResponseCode();

System.out.println(tempStr.toString());
System.out.println("----code:" + code);
}

}
...全文
210 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
crazylaa 2009-12-01
  • 打赏
  • 举报
回复
楼主试试用字节读,我这段都取下来了。

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class DownloadFile {

public static void getSong(String _path, String _savePath) {
String savePath = _savePath;
String path = _path;
int BYTE_SIZE = 1;
int SAVE_SIZE = 1024;
byte[] buff = new byte[BYTE_SIZE]; // 每次读的缓存
byte[] save = new byte[SAVE_SIZE]; // 保存前缓存
BufferedInputStream bf = null;
FileOutputStream file;
URL url = null;
HttpURLConnection httpUrl;
try {
url = new URL(path);
httpUrl = (HttpURLConnection) url.openConnection();
System.out.println("已经打开连接....");
bf = new BufferedInputStream(httpUrl.getInputStream());
System.out.println("已经获取资源......");
file = new FileOutputStream(savePath);
System.out.println("准备保存到:" + savePath);

System.out.println("开始读入......");
int i = 0;
while (bf.read(buff) != -1) { // 一个字节一个字节读
save[i] = buff[0];
if (i == SAVE_SIZE - 1) { // 达到保存长度时开始保存
file.write(save, 0, SAVE_SIZE);
save = new byte[SAVE_SIZE];
i = 0;
} else {
i++;
}
}
// 最后这段如果没达到保存长度,需要把前面的保存下来
if (i > 0) {
file.write(save, 0, i - 1);
}
System.out.println("下载成功!!!");
httpUrl.disconnect();
file.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {

bf.close();

} catch (Exception e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
try {
DownloadFile.getSong(
"http://wap.sonyericsson.com/UAprof/K700cR201.xml",
"D://1.xml");
DownloadFile.getSong(
"http://nds1.nds.nokia.com/uaprof/NN78-1r100.xml",
"D://2.xml");
DownloadFile.getSong("http://www.sohu.com", "D://3.xml");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
tongkuang 2009-12-01
  • 打赏
  • 举报
回复
IE里都是正常的
hechenqingtian 2009-12-01
  • 打赏
  • 举报
回复
兄弟 我也一样的问题,正在解决当中。java 和C#请求的结果不一样
caizhuxi 2009-12-01
  • 打赏
  • 举报
回复
在ie地址栏里试下url,看下结果,在看看程序执行有异常吗

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧