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).



Download: IT Certifications 101
Inside this exclusive essential guide, our independent experts break down which IT certifications are worth your time and effort, and how to get started obtaining them to further your career— including specific certifications that any cloud or desktop pro should seriously consider.
By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
You also agree that your personal information may be transferred and processed in the United States, and that you have read and agree to the Terms of Use and the Privacy Policy.
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(); } } }
Join the conversation
1 comment