JAVA:通过 HTTPS POST 数据似乎没有发送?

发布于 2024-08-25 16:10:48 字数 3556 浏览 2 评论 0原文

我在 Java 中通过 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());


    }
}

请求类:

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

I have a problem POSTing data via HTTPS in Java. The server response is the same whether or not I send 'query'. Maybe someone can point out what the problem is...

Thanks!

Main class:

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 class:

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 Class:

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: mostly from here: http://www.java-samples.com/java/POST-toHTTPS-url-free-java-sample-program.htm

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烙印 2024-09-01 16:10:48

您忘记了之后 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 教程,了解更多基本课程和指南。

You forgot to flush() or, better, close() the stream afterwards (which already does an implicit flush). Flushing is necessary because the data may be internally buffered to a certain length and flushing forces the entire buffer to be actually written to the output.

So, change

 output.writeBytes(query);

to

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

Actually, the normal idiom is to close streams in the finally block of the try block as you've acquired them, so that you can guarantee that they get closed whenever an IOException occurs during the write. Closing is important because it frees up system resources.

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.
}

Also see the Sun Java IO tutorial for more basic lessons and guidelines.

可遇━不可求 2024-09-01 16:10:48

乌尔克。您可能会被说服使用 Apache commons http 客户端 吗?

这个链接更直接。

这比调试这段代码要少很多工作。

Urk. Could you possibly be persuaded to use the Apache commons http client instead?

This link is more direct.

It's a lot less work than debugging this code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文