Send data through HTTP post requests with Java agent

Suppose that you have some documents by which you want to extract some data you need to send via HTTP POST requests.

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 ) {
			i++;
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 
					connection.setRequestMethod("POST"); 
					connection.setAllowUserInteraction(false); 
					connection.setDoOutput(true); 
					connection.setDoInput(true);
					connection.setUseCaches(false);
					// 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 ();
					connection.disconnect();

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

					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) {
e.printStackTrace();
}
}
}      
This was first published in October 2001

Dig deeper on Lotus Notes Domino Administration Tools

0 comments

Oldest 

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:

-ADS BY GOOGLE

SearchWinIT

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchEnterpriseLinux

SearchVirtualDataCentre.co.UK

Close