Jsoup不支持PUT也没有DELETE方法。由于它是一个解析器,它不需要支持这些操作。你可以做的是使用HttpURLConnection
,这与Jsoup在下面使用的是一样的。有了这个,你可以使用任何你想要的方法,最后用jsoup解析结果(如果你真的需要的话)。 检查这个代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) {
try {
String rawData = "RAW_DATA_HERE";
String url = "URL_HERE";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("METHOD_HERE"); //e.g POST
con.setRequestProperty("KEY_HERE", "VALUE_HERE"); //e.g key = Accept, value = application/json
con.setDoOutput(true);
OutputStreamWriter w = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
w.write(rawData);
w.close();
int responseCode = con.getResponseCode();
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 code : " + responseCode);
System.out.println(response.toString());
//Use Jsoup on response to parse it if it makes your work easier.
} catch(Exception e) {
e.printStackTrace();
}
}
}
感谢您的代码和解释。我知道HttpUrlConnection方法,但由于jsoup支持GET,POST方法,我期望它能够处理所有方法的完整性。 – 2014-09-15 07:08:27