我正在使用 Java 并尝试使用连接字符串形成 URL。然后将其作为 POST 的 URI 传递给 HttpPost。
此代码失败:
String str1 = "http://www.example.com";
String str2 = "/directory";
HttpPost httppost = new HttpPost(str1 + str2);
这也失败了:
HttpPost httppost = new HttpPost(str1.concat(str2));
就像这样:
String strCombined = str1 + str2;
HttpPost httppost = new HttpPost(strCombined);
但是,这有效:
HttpPost httppost = new HttpPost("http://www.example.com/directory");
我错过了什么?
编辑:堆栈跟踪:
05-31 01:43:29.973: W/System.err(451): java.net.UnknownHostException: http
05-31 01:43:29.983: W/System.err(451): at java.net.InetAddress.lookupHostByName(InetAddress.java:513)
05-31 01:43:29.993: W/System.err(451): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:278)
05-31 01:43:29.993: W/System.err(451): at java.net.InetAddress.getAllByName(InetAddress.java:242)
05-31 01:43:30.003: W/System.err(451): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
05-31 01:43:30.013: W/System.err(451): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-31 01:43:30.013: W/System.err(451): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-31 01:43:30.024: W/System.err(451): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
05-31 01:43:30.033: W/System.err(451): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-31 01:43:30.043: W/System.err(451): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-31 01:43:30.043: W/System.err(451): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)