2010-03-20 40 views
0

我在Java中通過HTTPS發佈數據時遇到問題。無論我是否發送「查詢」,服務器響應都是相同的。也許有人可以指出是什麼問題...JAVA:POST數據通過HTTPS似乎並沒有被髮送?

謝謝!

主要類:

package bind; 

public class Main { 

    public static final String urlString = "https://www.sms.ethz.ch/cgi-bin/sms/send.pl"; 

    public static void main(String[] args) { 

Message msg = new Message("Alles klar?"); 
URL url = new URL(urlString); 

String[][] values = new String[3][2]; 
values[0][0] = "action"; 
values[0][1] = "listoriginators"; 
values[1][0] = "username"; 
values[1][1] = "xxxxxx"; 
values[2][0] = "password"; 
values[2][1] = "xxxxxx"; 


Query query = new Query(values); 
System.out.println("Query: " + query.getQuery()); 

Request request = new Request(url.getURL(), query.getQuery()); 


    } 
} 

Request類:

package bind; 

public class Request { 
    static private int ic = 0; 
    private URL url; 

    protected Request(java.net.URL URL, String query){ 
ic++; 
if(CONSTANTS.SHOW_LOGS) { System.out.println("log: new instance of 'Message'"); } 

// connect 
try { 

    System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol"); 
    java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 

    javax.net.ssl.HttpsURLConnection connection = (javax.net.ssl.HttpsURLConnection) URL.openConnection(); 
    connection.setDoInput(true); 
    connection.setDoOutput(true); 
    connection.setRequestMethod("POST"); 
    connection.setFollowRedirects(true); 

    connection.setRequestProperty("Content-Length", String.valueOf(query.length())); 
    connection.setRequestProperty("Content-Type", "application/x-www- form-urlencoded"); 
    connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); 

    java.io.DataOutputStream output = new java.io.DataOutputStream(connection.getOutputStream()); 

    output.writeBytes(query); // <<-- NOTHING CHANGES IF I COMMENT THIS OUT OR NOT !!??!?! 

    System.out.println("log: response code: " + connection.getResponseCode()); 
    System.out.println("log: response message: " + connection.getResponseMessage()); 

    java.io.DataInputStream input = new java.io.DataInputStream(connection.getInputStream()); 
    for(int i = input.read(); i != -1; i = input.read()) { 
    System.out.print((char)i); 
    } 
    System.out.print("\n"); 
    input.close(); 

} catch(java.io.IOException e) { 
    if(CONSTANTS.SHOW_LOGS) { 
    System.out.println("error: unable to connect"); 
    System.out.println(e); 
    e.printStackTrace(); 
    } 
} 

    } 
} 

URL類別:

public class URL { 
    static private int ic = 0; 
    private String URLString; 
    private java.net.URL url; 

    protected URL(String a_url){ 
ic++; 

if(CONSTANTS.SHOW_LOGS) { System.out.println("log: new instance of 'URL'"); } 

setURLString(a_url); 
createURL(); 
    } 

    private void setURLString(String a_url) { 
URLString = a_url; 
    } 

    private void createURL() { 
try { 
    url = new java.net.URL(URLString); 
} catch(java.net.MalformedURLException e) { 
    System.out.println("error: invalid URL"); 
    System.out.println(e); 
    e.printStackTrace(); 
} 
    } 

    private void showURL() { 
System.out.println("URL: " + url.getHost() + url.getPath()); 
    } 

    public java.net.URL getURL() { 
return url; 
    } 

} 

PS:大多來自這裏:http://www.java-samples.com/java/POST-toHTTPS-url-free-java-sample-program.htm

回答

1

的Urk。你可能會被勸說使用Apache commons http client而不是?

This link更直接。

這比調試這段代碼要少很多。

+0

感謝您的提示。我會研究這一點。謝謝! – ostollmann 2010-03-20 11:52:30

+0

只是想感謝你提供有關Apache公共庫的技巧,它們非常棒!謝謝! – ostollmann 2010-03-25 16:40:11

2

您忘了flush()或更好的close()之後的流(它已經做了隱式刷新)。刷新是必要的,因爲數據可能在內部被緩衝到一定的長度,並且刷新迫使整個緩衝區實際寫入輸出。

所以,改變

output.writeBytes(query); 

output.writeBytes(query); 
output.close(); 

其實,正常的成語是關閉流在try塊的finally塊,你已經獲得它們,這樣就可以保證在寫入期間發生IOException時關閉。關閉非常重要,因爲它釋放了系統資源。

Stream stream = null; // Can be InputStream, OutputStream, Reader or Writer. 
try { 
    stream = new SomeStream(); 
    // ... 
} finally { 
    if (stream != null) try { stream.close(); } catch (IOException logOrIgnore) {} // You can if necessary bake an utility method taking Closeable or take a look for commons IO. 
} 

另請參閱Sun Java IO tutorial瞭解更多基礎知識和指導原則。

+0

感謝您的回答,但這並不會改變任何事情。之前我曾嘗試過(不知道爲什麼我再次移除它)。我可能會嘗試上面提到的Apache Commons HTTP Client bmargulies。 – ostollmann 2010-03-20 11:51:46

+0

在調用'connection.getInputStream()'之前,你確實調用了'output.close()'**,是嗎? – BalusC 2010-03-21 04:27:07

+0

對不起,遲到了,我一直在使用bmargulies提到的Apache libs,所以我放棄了這個問題。但是我在output.writeBytes(query)之後的行上添加了它,但仍然無效。 我認爲它可能只是與ssl有關......無論如何,我已經轉移到Apache庫,他們工作得很好。 感謝您的幫助:-) – ostollmann 2010-03-25 16:39:25