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();
}
}
}