Skip to content

Instantly share code, notes, and snippets.

@afandian
Created January 18, 2011 23:46
Show Gist options
  • Save afandian/785404 to your computer and use it in GitHub Desktop.
Save afandian/785404 to your computer and use it in GitHub Desktop.
WebserviceConsumerDemo
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" />
*/
}
}
@afandian
Copy link
Author

$ ls -l
total 8
drwxr-xr-x@ 144 joewass  staff  4896  9 May  2001 HTTPClient
-rw-r--r--@   1 joewass  staff  3569 18 Jan 23:41 WebserviceConsumerDemo.java
$ javac WebserviceConsumerDemo.java 
$ java WebserviceConsumerDemo
<status authenticated="no" />
<response success="pass" />
<status authenticated="yes" />
<response success="pass" />
<status authenticated="no" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment