Java-发送邮件

Java-发送邮件,java,eclipse,post,get,Java,Eclipse,Post,Get,我正试图让java成为url缩短器的一个新的补充,以下是我所拥有的: private void sendPost() throws Exception { String url = "http://shmkane.com/index.php?"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMeth

我正试图让java成为url缩短器的一个新的补充,以下是我所拥有的:

private void sendPost() throws Exception {

    String url = "http://shmkane.com/index.php?";
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

    String urlParameters = "url=testingThis";

    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    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();

    System.out.println(response.toString());

}
我希望它在“form1”中输入一些内容,然后再“submit”


我对此很陌生,所以任何帮助都会很好,可能会告诉我我做错了什么,或者改进/修复了我目前拥有的代码。

该网站不支持post:

<form method="get" id="form1" action="index.php">
    <p>&nbsp;</p>
    <p><br>
      <input class="textBox" id="url" placeholder="Paste a link to shorten it" type="text" name="url" value="">
    </p>
    <p>
      <input class="textBox" placeholder="Custom alias" id="alias" maxlength="15" type="text" name="alias" value="">
    </p>
    <div class="button">
      <p>
        <input class="button" type="submit" value="Shorten">
      </p>
<br>
      <div class="alert-box success">
        <center>
          <a href="" target="_blank"></a>
        </center>
      </div>
      <em>May only contain letters, numbers, dashes and underscores.</em>
      <p></p>
    </div>
  </form>

并且摆脱对输出流的写入。

您遇到了什么问题?我猜它没有提交请求,是不是引发了异常?它输出什么?你怎么知道它不工作了?当我检查网站是否添加了任何新链接时,它没有显示出来。没有错误。Console只是显示了这样一条消息:谢谢,这是有效的,所以为了清楚起见,请求的方法是否取决于网站上的表单类型?还有,我怎样才能从网站上获得它发送给我的链接?@shmkane是的,是的。它必须匹配HTTP服务器所期望的内容,以便能够相应地解析参数。有很多方法可以获取链接,但是您需要像现在这样读取响应,然后使用某种标准格式解析响应。我建议您寻找解析html的技巧。或者,查找返回json或xml的url。
 String urlParameters = "url=testingThis";
 String url = "http://shmkane.com/index.php?";
 URL obj = new URL(url + urlParameters);
 HttpURLConnection con = (HttpURLConnection) obj.openConnection();

 con.setRequestMethod("GET");