Showing posts with label IDXML. Show all posts
Showing posts with label IDXML. Show all posts

Wednesday, August 1, 2007

JUNIT Test Case for getting values from Oblix

============================
File: IDXMLUnitTest.java
============================

import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


import junit.framework.TestCase;


public class IDXMLUnitTest extends TestCase {


public IDXMLUnitTest(String name) {
super(name);
}

/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
}

/* (non-Javadoc)
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}

public void testSearch()
{
String url = "http://localhost:8081/identity/oblix/apps/userservcenter/bin/userservcenter.cgi?wsdl";
System.out.println(url);

String authen = "adminpassword";

String xml = ""+authen+"" +
"cn=RajnishBhatia,o=NetscapeRootmail" +
"
";

System.out.println("\n\n"+xml+"\n\n");


try
{

URL u = new URL(url);
System.out.println(" url .... " + url);
URLConnection c = u.openConnection();
HttpURLConnection conn = (HttpURLConnection) c;
conn.setRequestProperty("Content-Type", "text/xml");

conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStream out = conn.getOutputStream();
OutputStreamWriter wout = new OutputStreamWriter(out, "UTF-8");
wout.write(xml);
wout.flush();
out.close();

if(conn.getResponseCode() == 200) {
InputStream in = conn.getInputStream();
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new InputSource(in));
Element rootElement = document.getDocumentElement();
NodeList parentNode = rootElement.getElementsByTagName("ObValue");
Node node = parentNode.item(0);
System.out.println(" ...value ... " +node.getNodeName() + " .... " + node.getFirstChild().getNodeValue());
in.close();
}else
{
System.out.println(" conn.getResponseCode()" + conn.getResponseCode()) ;
}

conn.disconnect();
}catch(Exception e)
{
System.out.println("Exception ......." + e.getMessage());
}
}
}
==================
Result
==================
rajnishbhatia19@hotmail.com

courtesy:Dilip Nimse

Thursday, July 26, 2007

Calling A Web Service in .NET without adding a web reference

If you ever need to reach a web service and use its output in your codewithout adding a web reference in your project, here is the code you can use:

I'm posting a code I used to connect to Netpoint Oblix CoreID Oracle Access Manager and retrieve values from it. This is a console app sample. You can use the same code in your web apps as well.
=====================================================
File Program.cs
=====================================================
using System;
using System.Xml;
namespace IDXML
{
class Program
{
private const string m_InFileName ="C:\\Projects\\IDXML\\IDXML\\InputXML.xml";
static void GetOblixData(string URL)
{
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(m_InFileName);
Byte[] bdata =System.Text.Encoding.ASCII.GetBytes(xDoc.OuterXml);
System.Net.WebClient wc = newSystem.Net.WebClient();
wc.Headers.Add("Content-Type","text/xml");
Byte[] bresp;
bresp = wc.UploadData(URL, bdata);
string resp =System.Text.Encoding.ASCII.GetString(bresp);
//Console.WriteLine(resp);
XmlDocument xresp = new XmlDocument();
xresp.LoadXml(resp);
XmlNodeList elems =xresp.GetElementsByTagName("ObValue");
XmlNode x = elems[0]; // Assuming One / Picking First - ... If more then just loop
Console.WriteLine(x.InnerText);
}
catch(Exception ex)
{

Console.WriteLine(ex.Message+""+ex.StackTrace);
}
}

static void Main(string[] args)
{
try
{

GetOblixData("http://localhost:8081/identity/oblix/apps/userservcenter/bin/userservcenter.cgi?wsdl");
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
}
}


=====================================================
File Input.xml (I used this file to provide input xml sothat we can change it. You can simply prepare an xml in your code or read from somewhere else).
=====================================================

<?xml version="1.0"?>
<SOAP-ENV:Envelopexmlns:SOAP-ENV="http://schemas-xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<oblix:authenticationxmlns:oblix="http:/www.oblix.com" type="basic">
<oblix:login>admin</oblix:login>
<oblix:password>password</oblix:password>
</oblix:authentication>
<oblix:request application="userservcenter"function="view" version="NPWSDL1.0"xmlns:oblix="http://www.oblix.com">
<oblix:params>
<oblix:uid>cn=RajnishBhatia,o=NetscapeRoot</oblix:uid>
<oblix:attrName>mail</oblix:attrName>
</oblix:params>
</oblix:request>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
=====================================================
Result
=====================================================
rajnishbhatia19@hotmail.com