?
Solved

XML API with Java

Posted on 2011-05-04
11
Medium Priority
?
302 Views
Last Modified: 2012-05-11
Hi I waan use xml api with java.

I can do this before with c#.

This is the code.


//The url command to do what you need
string url = "http://your-whm-server:2086/xml-api/suspendacct?user=johndoe&reason=no%20payment";

WebRequest wreq = WebRequest.Create(url);

//Your credentials (the same you use to login on WHM panel)
CredentialCache MyCredential = new CredentialCache();
MyCredential.Add(new Uri("http://your-whm-server.com:2086/"), "Basic", new NetworkCredential("your-whm-username", "your-whm-password"));
wreq.Credentials = MyCredential;

//I set the timeout to 8 seconds...
wreq.Timeout = 8000;

WebResponse wres = wreq.GetResponse();
if (((HttpWebResponse)wres).StatusDescription == "OK")
{
      wres.Close(); //to free resources
      //Code if OK
}
else
{
      wres.Close(); //to free resources
      //Code if not OK
}

How can I do this. with JAVA ?

 and idea ?
0
Comment
Question by:Absente
  • 6
  • 4
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35693121
You can use a URLConnection with an Authenticator

http://www.javaworld.com/javaworld/javatips/jw-javatip47.html

(ignore the gui parts)
0
 
LVL 92

Expert Comment

by:objects
ID: 35695005
you need to set a request property in your request like in the follwoing:

http://helpdesk.objects.com.au/java/how-to-specify-authentication-details-for-proxy-server

but in your case it would be Authorization instesad of Proxy-Authorization:


URLConnection connection = url.openConnection();
String authentication = username"+":"+password;
String encodedAuthentication = base64Encode(authentication);
connection.setRequestProperty("Authorization", "Basic "+encodedAuthentication);

Open in new window

0
 

Author Comment

by:Absente
ID: 35747667
thank you. it is return some xml string.

how can i parse it. what is the best way ?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 35748268
See http://www.exampledepot.com/egs/javax.xml.parsers/BasicDom.html but do instead
Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xmlString))));

Open in new window

0
 

Author Comment

by:Absente
ID: 35748360
I want to parse.  for example i want to parse and set to table.

dont want to create file.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35748419
You don't need to create a file (hence my alteration)
0
 

Author Comment

by:Absente
ID: 35748449
so .  forexample, How can i  parse xml string into arraylist ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35748465
What's the content of the xml
0
 

Author Comment

by:Absente
ID: 35748485
<listaccts>
    <acct>
        <disklimit>unlimited</disklimit>
        <diskused>57884M</diskused>
        <domain>example.com</domain>
        <email>user@example.com</email>
        <ip>192.168.0.1</ip>
        <maxaddons>10</maxaddons>
        <maxftp>unlimited</maxftp>
        <maxlst>unlimited</maxlst>
        <maxparked>unlimited</maxparked>
        <maxpop>unlimited</maxpop>
        <maxsql>unlimited</maxsql>
        <maxsub>unlimited</maxsub>
        <owner>root</owner>
        <partition>home</partition>
        <plan>default</plan>
        <shell>/bin/bash</shell>
        <startdate>09 Aug 13 15:01</startdate>
        <suspended>0</suspended>
        <suspendreason>not suspended</suspendreason>
        <suspendtime/>
        <theme>x3</theme>
        <unix_startdate>1250193678</unix_startdate>
        <user>user</user>
    </acct>
</listaccts>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35748499
It would be better to use an Account bean. Take a look at Commons Digester. JAXB might be overkill
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35907495
:)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question