日期:2014-05-17  浏览次数:20722 次

java访问url
我在浏览器访问
http://lab.3haku.net/f/do?phone=15855022871&pwd=liu15855022871&to=15855022871&u=utf8&msg=game over
手机能收到 game over
但是我copy来的程序只能接受到game为什么呀?怎么改。。。
代码如下;import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class Fetion {

  public static void main(String[] args) {
  String urlStr = "http://lab.3haku.net/f/do?phone=15855022871&pwd=liu15855022871&to=15855022871&u=utf8&msg=game over";  
  URL url;
  try {
  url = new URL(urlStr);
  URLConnection URLconnection = url.openConnection();  
  HttpURLConnection httpConnection = (HttpURLConnection)URLconnection;  
  int responseCode = httpConnection.getResponseCode();  
  if (responseCode == HttpURLConnection.HTTP_OK) {  
  System.err.println("成功");
  InputStream urlStream = httpConnection.getInputStream();  
  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlStream));  
  String sCurrentLine = "";  
  String sTotalString = "";  
  while ((sCurrentLine = bufferedReader.readLine()) != null) {  
  sTotalString += sCurrentLine;  
  }  
   


  System.err.println( sTotalString);
   
   
  } 
   
  }catch (Exception e) {
  // TODO Auto-generated catch blockeb
  e.printStackTrace();
  }  
}
}



------解决方案--------------------
参数中有空格的原因
传参前用java.net.URLEncoder.encode对参数进行编码或是把空格替换成%20
------解决方案--------------------
楼上答案很对!

String urlStr = "http://lab.3haku.net/f/do?phone=15855022871&pwd=liu15855022871&to=15855022871&u=utf8&msg=game over"; 


------解决方案--------------------
学习了哈