【发布时间】:2014-01-23 10:51:29
【问题描述】:
我有下面的小代码来获取服务提供商的 json 回复,我尝试的是发布一个 http post 请求,但它总是抛出
java.net.UnknownHostException: directory.qantasloyalty.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:382)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:509)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:278)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:335)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:176)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:769)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:162)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:861)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230)
at Test.Httptestpost.sendPost(Httptestpost.java:124)
at Test.Httptestpost.main(Httptestpost.java:32)
请在下面找到我的代码,
private void sendPost() {
System.getProperties().put("http.proxySet", "true");
System.getProperties().put("http.proxyHost", "proxyurl");
System.getProperties().put("http.proxyPort", "8080");
System.getProperties().put("http.proxyUser", "username");
System.getProperties().put("http.proxyPassword", "pwd");
System.getProperties().put("http.nonProxyHosts", "localhost|127.0.0.1");
String url = "httpsurlhere";
URL obj = null;
try {
obj = new URL(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpsURLConnection con = null;
try {
con = (HttpsURLConnection) obj.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//add reuqest header
try {
con.setRequestMethod("POST");
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//con.setRequestProperty("User-Agent", USER_AGENT);
//con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Content-Type", "application/json;UTF-8");
String urlParameters = "username=demouser&password=aDemoPassword";
String input = "{\"username\":\"demouser\",\"password\":\"aDemoPassword\"}";
System.out.println("input"+input);
System.out.println("url"+con.getURL());
System.out.println("req prop :"+con.getRequestProperties());
// Send post request
con.setDoOutput(true);
DataOutputStream wr;
try {
wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(input);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
有人可以帮我解决原因
为了安全起见,我没有在这里分享确切的代理 URL 和 http post webservice URl
【问题讨论】:
-
你的代码没有对齐异常,你在代码中使用的url是任意字符串
httpsurlhere,还有其他代理参数 -
不,伙计,我只使用正确的 url 和正确的代理设置,仅出于安全目的,我在这些地方放了一些任意字符串
标签: java json web-services http http-post