Java URL Post xmlPost(String urlStr, String xmlInfo)

Here you can find the source of xmlPost(String urlStr, String xmlInfo)

Description

xml Post

License

Apache License

Declaration

public static String xmlPost(String urlStr, String xmlInfo) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static String xmlPost(String urlStr, String xmlInfo) {
        try {//  w  ww . ja v  a2  s .  co m
            URL url = new URL(urlStr);
            URLConnection con = url.openConnection();
            con.setDoOutput(true);
            con.setRequestProperty("Pragma:", "no-cache");
            con.setRequestProperty("Cache-Control", "no-cache");
            con.setRequestProperty("Content-Type", "text/xml");

            OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
            System.out.println("urlStr=" + urlStr);
            System.out.println("xmlInfo=" + xmlInfo);
            out.write(new String(xmlInfo.getBytes("ISO-8859-1")));
            out.flush();
            out.close();
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String line = "";
            for (line = br.readLine(); line != null; line = br.readLine()) {
                line += line;
            }
            return line;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}

Related

  1. postRequest(URLConnection conn, Map nameValuePairs)
  2. postToUrl(String page)
  3. postUrl(URL url, String data)
  4. writePostRequest(URLConnection connection, String postRequestBody, String contentType)
  5. writeString(URLConnection connection, String string)