我正在尝试让java为url shortener添加一个新功能,这就是我所拥有的:
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”中,然后“提交” here's the site
我对此非常陌生,所以任何帮助都会很棒,可能会告诉我我做错了什么或改进/修复我目前的代码。
答案 0 :(得分:0)
该网站不支持发布:
<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");
摆脱对输出流的写入。