我正在尝试让 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”中放入一些内容,然后“提交”
我对此很陌生,所以任何帮助都会很棒,可能会告诉我我做错了什么或改进/修复我当前拥有的代码。
该网站不支持发帖:
<form method="get" id="form1" action="index.php">
<p> </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>
注意方法是
GET
。 改成这样:
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");
并摆脱对输出流的写入。