posting XML request in java

java

Here's an example how to do it with java.net.URLConnection:

String url ="http://example.com";
String charset ="UTF-8";
String param1 =URLEncoder.encode("param1", charset);
String param2 =URLEncoder.encode("param2", charset);
String query =String.format("param1=%s&param2=%s", param1, param2);

URLConnection urlConnection =new URL(url).openConnection();
urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true);// Triggers POST.
urlConnection.setRequestProperty("accept-charset", charset);
urlConnection.setRequestProperty("content-type","application/x-www-form-urlencoded");

OutputStreamWriter writer =null;
try{
    writer =newOutputStreamWriter(urlConnection.getOutputStream(), charset);
    writer.write(query);// Write POST query string (if any needed).
}finally{
    if(writer !=null)try{ writer.close();}catch(IOException logOrIgnore){}
}

InputStream result = urlConnection.getInputStream();
// Now do your thing with the result.
// Write it into a String and put as request attribute
// or maybe to OutputStream of response as being a Servlet behind `jsp:include`.

以上是 posting XML request in java 的全部内容, 来源链接: utcz.com/z/394595.html

回到顶部