This is my first time writing a web-service which invokes a URL. At this time I am trying to invoke a friends url to post xml file content to it. Testing it out, it seemed to work fine as I could invoke a web address such as www.google.com. The address of his I recently received is "http://ift.tt/UNIzUE. When I try and invoke this web address I received a "java.net.unknownhostexception: http://ift.tt/UNIzUE" error. Does anyone have tips on how I could alter my code to paste XML content to this address? Thanks.
public class PostXml {
public static void main(String[] args) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
InputStream inputStream = new FileInputStream(new File(args[0]));
org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream);
StringWriter stw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
serializer.transform(new DOMSource(doc), new StreamResult(stw));
String xmldata = (stw.toString());
//System.out.println(xmldata);
System.out.println("Post_Request:\n");
System.out.println(xmldata);
//Create socket
String hostname = args[1];
int port = 80;
InetAddress addr = InetAddress.getByName(hostname);
Socket sock = new Socket(addr, port);
//Send header
String path = "/path";
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(),"UTF-8"));
// You can use "UTF8" for compatibility with the Microsoft virtual machine.
wr.write("POST " + path + " HTTP/1.0\r\n");
wr.write("Host: HostName\r\n");
wr.write("Content-Length: " + xmldata.length() + "\r\n");
wr.write("Content-Type: text/xml; charset=\"utf-8\"\r\n");
wr.write("\r\n");
wr.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
wr.write("<soap:Envelope xmlns:xsi=\"http://ift.tt/ra1lAU\" xmlns:xsd=\"http://ift.tt/tphNwY\" xmlns:soap=\"http://ift.tt/sVJIaE\">\r\n");
wr.write("<soap:Body>\r\n");
wr.write("<CreateNewPR xmlns=\"path\">\r\n");
wr.write(xmldata + "\r\n");
wr.write("</CreateNewPR>");
wr.write("</soap:Body>\r\n");
wr.write("</soap:Envelope>\r\n");
//Send data
wr.flush();
System.out.println("\nGet_Response printed in txt file.");
//Prints response to file
File file = new File(args[2]);
FileOutputStream fis = new FileOutputStream(file);
PrintStream out = new PrintStream(fis);
System.setOut(out);
// Response
BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String line;
while((line = rd.readLine()) != null)
System.out.println(line);
} catch (Exception e) {
e.printStackTrace();
}
}
}
arg 0 is my xml file, arg 1 is my asmx adress, arg 2 is my output file.txt
No comments:
Post a Comment