DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Convert XLS to XLSX in Java
  • Thread-Safety Pitfalls in XML Processing
  • How Java Servlets Work: The Backbone of Java Web Apps
  • How to Merge Excel XLSX Files in Java

Trending

  • Production-Grade LLM Regression Testing: Contracts, Evals, and Quality Gates
  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust
  • What Cloud Engineers Actually Need to Know About AI Infrastructure
  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Using Java, POST A Block Of XML To A Web Server

Using Java, POST A Block Of XML To A Web Server

By 
Douglas Wyatt
·
Mar. 04, 09 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
22.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in
// This is a working example of POSTing a string (representing a block of XML) to a web server


try
{
    URL url = new URL( argUrl );
    URLConnection con = url.openConnection();

    // specify that we will send output and accept input
    con.setDoInput(true);
    con.setDoOutput(true);

    con.setConnectTimeout( 20000 );  // long timeout, but not infinite
    con.setReadTimeout( 20000 );

    con.setUseCaches (false);
    con.setDefaultUseCaches (false);

    // tell the web server what we are sending
    con.setRequestProperty ( "Content-Type", "text/xml" );

    OutputStreamWriter writer = new OutputStreamWriter( con.getOutputStream() );
    writer.write( requestXml );
    writer.flush();
    writer.close();

    // reading the response
    InputStreamReader reader = new InputStreamReader( con.getInputStream() );

    StringBuilder buf = new StringBuilder();
    char[] cbuf = new char[ 2048 ];
    int num;

    while ( -1 != (num=reader.read( cbuf )))
    {
        buf.append( cbuf, 0, num );
    }

    String result = buf.toString();
    System.err.println( "\nResponse from server after POST:\n" + result );
}
catch( Throwable t )
{
    t.printStackTrace( System.out );
}
XML Web server Java (programming language) POST (HTTP)

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert XLS to XLSX in Java
  • Thread-Safety Pitfalls in XML Processing
  • How Java Servlets Work: The Backbone of Java Web Apps
  • How to Merge Excel XLSX Files in Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook