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