Send data through HTTP post requests with Java agent

Suppose that you have some documents in a categorized view, by which you want to extract some data you need to send via HTTP POST requests. The agent performs a search in a view, named [VIEWNAME], getting all these documents whose category has a particular value[CATEGORYVALUE]. Then, for every document matching the condition over the category value, it builds a string(content) into which you can add all the params you have to send, in querystring form(param1=value1¶m2=value2...). Next, it opens a connection to the url([URL]) you have to call to send the data to, writes the data on a DataOutputStream object, and closes the connection. Once it has sent the HTTP Post request, if the url sends a response, the agent is able to catch it, creating a BufferedReader object, from which it writes on the server console(or on the NotesLOG database on the server which the agent runs on).

import lotus.domino.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class JavaAgent extends AgentBase {

public void NotesMain() {

try {
	Session session = getSession();
      AgentContext agentContext = session.getAgentContext();
      Database db = agentContext.getCurrentDatabase();
	 View view = db.getView("[VIEWNAME]");
      DocumentCollection dc = view.getAllDocumentsByKey("[CATEGORYVALUE]", true);
      int ndocs = dc.getCount();
int i = 0;
	Document doc = dc.getFirstDocument();
	while( doc != null ) {
String field1 = doc.getItemValueString("field1");
					String enc_field1 = URLEncoder.encode(field1);
	...........................................................................(more fields)
String content= "field1="+enc_field1+"&&enc_field2="+enc_field2+...........(more params);
	 				URL url = new URL("[URL]");
					HttpURLConnection connection = (HttpURLConnection)url.openConnection();
					// Sets the request method to POST, and enable to send data 
					// Sets the defaul Content-type and content lenght for POST requests 
					connection.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); 
					connection.setRequestProperty( "Content-length", Integer.toString(content2.length())); 

					DataOutputStream out = new DataOutputStream (connection.getOutputStream ());
					out.writeBytes (content);
					out.flush ();
					out.close ();

					BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
					String line;
					while(null != (line = in.readLine())) {

					String title = db.getTitle();
      	 			System.out.println("Current database is "" + title + """);
			doc = dc.getNextDocument();
}catch(NotesException ne) {
System.out.println("NotesError " + ne.text);}catch(Exception e) {

This was first published in October 2001

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.