Created
January 18, 2011 23:46
-
-
Save afandian/785404 to your computer and use it in GitHub Desktop.
WebserviceConsumerDemo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import HTTPClient.*; | |
import java.io.*; | |
/* trying to get this bit to work. Without it you get an annoying dialogue box asking about cookies permission. | |
class FolktunePolicyHandler implements CookiePolicyHandler | |
{ | |
boolean acceptCookie(Cookie cookie, RoRequest req, RoResponse resp) | |
{ | |
// yeah whatever | |
return true; | |
} | |
boolean sendCookie(Cookie cookie, RoRequest req) | |
{ | |
return true; | |
} | |
} | |
*/ | |
public class WebserviceConsumerDemo | |
{ | |
private static BufferedReader Retrieve(String url, HTTPConnection connection, String postData) throws Exception | |
{ | |
HTTPResponse response; | |
if (postData == null) | |
{ | |
response = connection.Get(url); | |
} | |
else | |
{ | |
byte[] requestBytes = postData.getBytes(); | |
HttpOutputStream out = new HttpOutputStream(requestBytes.length); | |
response = connection.Post(url, out); | |
out.write(requestBytes); | |
out.close(); | |
} | |
BufferedReader reader = null; | |
// If it's not OK return null. | |
if (response.getStatusCode() == 200) | |
{ | |
InputStream inputStream = response.getInputStream(); | |
reader = new BufferedReader(new InputStreamReader(inputStream)); | |
} | |
return reader; | |
} | |
public static void main(String[] args) throws Exception | |
{ | |
// First take one of these | |
HTTPConnection connection = new HTTPConnection("www.folktune.local"); | |
/* Again, this bit is excludd for the minute | |
CookiePolicyHandler policyHandler = new FolktunePolicyHandler(); | |
CookieModule.setCookiePolicyHandler(policyHandler); | |
*/ | |
String line; | |
BufferedReader reader; | |
/* Q1: Are we logged in? */ | |
reader = Retrieve("/api/session/status/", connection, null); | |
while ((line = reader.readLine()) != null) | |
{ | |
System.out.println(line); | |
} | |
/* A1: What did you expect? | |
<status authenticated="no" /> | |
*/ | |
/* Q2: Can I log in please? */ | |
String loginXml = "<request username=\"abcnotation_com\" password=\"5fec5e9bbc3e041512460456e1269959\" />"; | |
reader = Retrieve("/api/session/login/", connection, loginXml); | |
while ((line = reader.readLine()) != null) | |
{ | |
System.out.println(line); | |
} | |
/* A2: Looks like it: | |
<response success="pass" /> | |
*/ | |
/* Q3: So are we actually logged in? */ | |
reader = Retrieve("/api/session/status/", connection, null); | |
while ((line = reader.readLine()) != null) | |
{ | |
System.out.println(line); | |
} | |
/* A3: What did you expect? | |
<status authenticated="yes" /> | |
*/ | |
/**** INTERLUDE. Do all your work here. ***/ | |
/* A4: Log out. */ | |
reader = Retrieve("/api/session/logout/", connection, null); | |
while ((line = reader.readLine()) != null) | |
{ | |
System.out.println(line); | |
} | |
/* A4: 'k. | |
<response success="pass" /> | |
*/ | |
/* Q5: Did that work?? */ | |
reader = Retrieve("/api/session/status/", connection, null); | |
while ((line = reader.readLine()) != null) | |
{ | |
System.out.println(line); | |
} | |
/* A5: Think so. | |
<status authenticated="no" /> | |
*/ | |
} | |
} |
Author
afandian
commented
Jan 18, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment