HttpURLConnection请求无结果问题
请求两个地址,其中第一个可以返回结果,但第二个地址无结果,
但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);
}
}