简体   繁体   English

你如何在Java中执行http帖子?

[英]how do you execute an http post in Java?

Here is the code I am using. 这是我正在使用的代码。 Instead of posting data to the page, it is downloading it like a regular text file. 它不是将数据发布到页面,而是像常规文本文件一样下载它。 So, I am actually getting the html code returned and the form is not being submitted. 所以,我实际上是返回了html代码并且没有提交表单。

I know the html form works. 我知道html表单有效。 If I open it in a browser, I can post to it and my data appears in my database. 如果我在浏览器中打开它,我可以发布到它,我的数据出现在我的数据库中。 I'm guessing I'm missing a parameter or something. 我猜我错过了参数或其他东西。

public void testComm ()
{
try
    {
    URL         url;
    URLConnection   urlConn;
    DataOutputStream    printout;
    DataInputStream input;

    url = new URL ("http://mysite.com/myform.html");

    // URL connection channel.
    urlConn = url.openConnection();

    // Let the run-time system (RTS) know that we want input.
    urlConn.setDoInput (true);

    // Let the RTS know that we want to do output.
    urlConn.setDoOutput (true);

    // No caching, we want the real thing.
    urlConn.setUseCaches (false);

    // Specify the content type.
    urlConn.setRequestProperty
    ("Content-Type", "application/x-www-form-urlencoded");

    // Send POST output.
    printout = new DataOutputStream (urlConn.getOutputStream ());

    String content =
    "txtLevelName=" + URLEncoder.encode ("level1") +
    "&txtLevelData=" + URLEncoder.encode ("abcd");

    printout.writeBytes (content);
    printout.flush ();
    printout.close ();

    // Get response data.
    input = new DataInputStream (urlConn.getInputStream ());

    String str;
    while (null != ((str = input.readLine())))
    {
    System.out.println (str);
    }

    input.close ();

    }
catch (MalformedURLException me)
    {
    System.err.println("MalformedURLException: " + me);
    }
catch (IOException ioe)
    {
    System.err.println("IOException: " + ioe.getMessage());
    }
}   

You're already sending a POST request. 您已经发送了POST请求。 It's the urlConn.setDoOutput(true) which sets the request method to POST. 它是urlConn.setDoOutput(true) ,它将请求方法设置为POST。 Apparently you're sending the request to the wrong URL. 显然,您将请求发送到错误的URL。 Open the HTML page in your browser. 在浏览器中打开HTML页面。 Rightclick and View Source . 右键单击并查看源代码 Locate the HTML <form> element in the HTML source. 在HTML源代码中找到HTML <form>元素。 It should look something like: 它应该看起来像:

<form action="http://mysite.com/somescript" method="post">

Check its action attribute. 检查其action属性。 That's exactly the URL which you have to POST to. 这正是你要发布到的URL。

See also: 也可以看看:

Probably you are not doing it right. 可能你做得不对。 Looks like you are posting it to the form instead of the action. 看起来您将其发布到表单而不是操作。

what I'm doing (I know there are frameworks for this, but i wanted it light-weight): 我正在做什么(我知道有这个框架,但我希望它轻量级):

        final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        if(!postString.isEmpty()) {
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length", Integer.toString(postString.getBytes().length));
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            final OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
            wr.write(postString);
            wr.flush();
            wr.close();
        }

postString is the same as your content string postString与您的内容字符串相同

// edit: oh and as sid mentioned above: your url is a html page??? //编辑:哦和上面提到的sid:你的网址是一个html页面???

you can try jsoup : http://jsoup.org/ 你可以试试jsouphttp//jsoup.org/

it's VERY easy to use: 它非常容易使用:

Document doc = Jsoup.connect("http://example.com")
  .data("query", "Java")
  .post();

Load a Document from a URL: http://jsoup.org/cookbook/input/load-document-from-url 从URL加载文档: http//jsoup.org/cookbook/input/load-document-from-url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM