Java code using HTTPURLConnect

Discussion about Schedules Direct grabber code and data formats.

Java code using HTTPURLConnect

Postby gwoverholt » Sun Dec 15, 2013 8:00 pm

Here's a snippet of code I spent a weekend of googling and trial and error in an attempt to download the xml guide data using a hand coded soap message and no external libraries. One area I had trouble with is the authentication piece. I've seen it listed in the forums that schedules direct accepts basic type of authentication when in reality it doesn't. The digest style of authentication is what seems to be required. I discovered this using wget with the debug option (an example of this is listed else where on this forum). It's worth mentioning that in my research to determine how to negotiate digest authentication using HttpURLConnection most believed that this class could only handle the basic authentication. This is not true but you need to set the default Authenticator with an instance of Authenticator where the getPasswordAuthentication method is overridden. This technique is not very intuitive; However, it is very easy to implement as you can see in my sample code. For some reason doing this triggers URLConnection to negotiate digest authentication when necessary.

If the sample code looks similar to other code you've seen out there on other forums you are probably correct as it was cobbled together by copy and pasting snippets from random postings. Anyhow, I hope it's useful:

Code: Select all
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.ProtocolException;
import java.net.URL;

public class SchedDirect {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        HttpURLConnection con = null;
        URL oURL;
       
        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication pa = new PasswordAuthentication ("YourSDUserName", "YourPassword".toCharArray());
                System.out.println("Credintials->" + pa.getUserName() + ":" + new String(pa.getPassword()));
                return pa;
            }
        });

        try {
            oURL = new URL("http://webservices.schedulesdirect.tmsdatadirect.com/schedulesdirect/tvlistings/xtvdService");
           
            con = (HttpURLConnection) oURL.openConnection();
            System.out.println(con.getPermission().toString());
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-type", "text/xml; charset=utf-8");
            //con.setRequestProperty("Accept-Encoding", "gzip");
            con.setDoOutput(true);
            con.setDoInput(true);

            String reqXML = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n"
                    + "   <SOAP-ENV:Body>\n"
                    + "      <m:download xmlns:m=\"urn:TMSWebServices\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
                    + "         <startTime xsi:type=\"xsd:dateTime\">2013-12-15T19:00:00Z</startTime>\n"
                    + "         <endTime xsi:type=\"xsd:dateTime\">2013-12-15T21:00:00Z</endTime>\n"
                    + "      </m:download>\n"
                    + "   </SOAP-ENV:Body>\n"
                    + "</SOAP-ENV:Envelope>";

            OutputStream reqStream = con.getOutputStream();
            reqStream.write(reqXML.getBytes());
            reqStream.flush();

            InputStream resStream = con.getInputStream();
//BufferedReader would probably be better here so you can use readline instead of reading into a bytebuffer unless you want to use the zip option then this
//is probably the best way to go.
            byte[] byteBuf = new byte[10240];
            int len;
            boolean blDone = false;
            StringBuilder sbResp = new StringBuilder();
            String sCurrent;
            while(!blDone){
                len = resStream.read(byteBuf);
                blDone = (len == -1);
               
                if(!blDone){
                    sCurrent = new String(byteBuf,0,len);
                    sbResp.append(sCurrent);
                }
               
            }
           
            reqStream.close();
            resStream.close();
            System.out.println(sbResp.toString());
        } catch(ProtocolException e){
            System.out.println("Protocol Exception:");
            e.printStackTrace();
           

        } catch (MalformedURLException e) {
            System.out.println("MalformedURL Exception:");
            e.printStackTrace();
           

        } catch (IOException e) {
            System.out.println("IOException:");
            e.printStackTrace();

        }

    }

}
gwoverholt
 
Posts: 1
Joined: Sun Dec 15, 2013 12:56 am

Return to Developers Corner

Who is online

Users browsing this forum: No registered users and 2 guests

cron