URLConnection to do XML post not working

N

Neil Aggarwal

Hello:

This class posts and XML request to ProPay's test server and generates
the correct response:

import java.io.*;
import java.net.*;

public class DirectSocketTest {
public static final String PROPAY_SERVER = "devtst.propay.com";
public static final String PROPAY_SERVER_PATH =
"/xml/xml.exe/process";

/** Creates a new instance of DirectSocketTest */
public DirectSocketTest() throws Exception {
// Connect a socket to the propay server
Socket socket = new Socket(PROPAY_SERVER,80);
// Create a writer to the socket
PrintWriter writer = new PrintWriter(new
OutputStreamWriter(socket.getOutputStream()));
// Get a reader from the socket
BufferedReader reader = new BufferedReader(new
InputStreamReader(socket.getInputStream()));

// Create the request for the propay server
String content = "<?xml version= \"1.0\"?>";
content += "<!DOCTYPE Request.dtd>";
content += "<XML Request>";
content += "
<username>[email protected]</username>";
content += "
<certStr>MadroneTestCertStraaaaaaaaaaaa</certStr>";
content += " <class>partner</class>";
content += " <XMLTrans>";
content += " <transType>05</transType>";
content += " <amount>9997</amount>" ;
content += " <sourceEmail>[email protected]</sourceEmail>"
;
content += " <ccNum>1544444444443333</ccNum>" ;
content += " <expDate>0102</expDate>" ;
content += " <CVV2>312</CVV2>" ;
content += " <invNum>G67613E</invNum>" ;
content += " </XMLTrans>" ;
content += "</XMLRequest>" ;

writer.println("POST "+PROPAY_SERVER_PATH+" HTTP/1.0");
writer.println("From: (e-mail address removed)");
writer.println("User-Agent: HTTPTool/1.0");
writer.println("Content-Type: text/xml; charset:ISO-8859-1;");
writer.println("Content-Length: "+content.length());
writer.println();
writer.println(content);
writer.println();
writer.flush();

// Read the result
String line = reader.readLine();
while( line != null ) {
System.out.println( line );
line = reader.readLine();
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
new DirectSocketTest();
}
}

Here is the output:
HTTP/1.1 200 OK
Server: Microsoft-IIS/4.0
Date: Sun, 18 Apr 2004 02:35:44 GMT
Content-Type: text/xml
Content-Length: 391
Content:

<?xml version="1.0" standalone="no"?>
<XMLResponse xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.propay.com/schema/PPResponse.xsd">
<XMLTrans>
<transType>05</transType>
<sourceEmail>[email protected]</sourceEmail>
<invNum>G67613E</invNum>
<status>48</status>
<transNum></transNum>
<authCode></authCode>
</XMLTrans>
</XMLResponse>

I wrote this class to do the same thing using the java.net classes:
import java.io.*;
import java.net.*;

public class PropayTest {
public static final String PROPAY_URL =
"http://devtst.propay.com/xml/xml.exe/process";

/** Creates a new instance of PropayTest */
public PropayTest() throws Exception {

// Create the request for the propay server
String content = "<?xml version= \"1.0\"?>";
content += "<!DOCTYPE Request.dtd>";
content += "<XML Request>";
content += "
<username>[email protected]</username>";
content += "
<certStr>MadroneTestCertStraaaaaaaaaaaa</certStr>";
content += " <class>partner</class>";
content += " <XMLTrans>";
content += " <transType>05</transType>";
content += " <amount>9997</amount>" ;
content += " <sourceEmail>[email protected]</sourceEmail>"
;
content += " <ccNum>1544444444443333</ccNum>" ;
content += " <expDate>0102</expDate>" ;
content += " <CVV2>312</CVV2>" ;
content += " <invNum>G67613E</invNum>" ;
content += " </XMLTrans>" ;
content += "</XMLRequest>" ;

// Connect to the propay url
URL url = new URL(PROPAY_URL);

URLConnection connection = url.openConnection();
if( connection instanceof HttpURLConnection )
((HttpURLConnection)connection).setRequestMethod("POST");
connection.setRequestProperty("Content-Length",
Integer.toString(content.length()) );
connection.setRequestProperty("Content-Type","text/xml;
charset:ISO-8859-1;");
connection.setDoOutput(true);
connection.connect();

// Create a writer to the url
PrintWriter writer = new PrintWriter(new
OutputStreamWriter(connection.getOutputStream()));
// Get a reader from the url
BufferedReader reader = new BufferedReader(new
InputStreamReader(connection.getInputStream()));

// Send the content to the propay server
writer.println();
writer.println(content);
writer.println();
writer.flush();

String line = reader.readLine();
while( line != null ) {
System.out.println( line );
line = reader.readLine();
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
new PropayTest();
}
}

But I get this output:

<?xml version="1.0" standalone="no"?>
<XMLResponse xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.propay.com/schema/PPResponse.xsd">
</XMLResponse>


Any ideas on why the java.net version of the code does not act the
same?

Thanks
Neil
 
L

Lothar Kimmeringer

((HttpURLConnection)connection).setRequestMethod("POST");
unnecessary

connection.setRequestProperty("Content-Length",
Integer.toString(content.length()) );
unnecessary

// Create a writer to the url
PrintWriter writer = new PrintWriter(new
OutputStreamWriter(connection.getOutputStream()));
// Get a reader from the url
BufferedReader reader = new BufferedReader(new
InputStreamReader(connection.getInputStream()));

you should open that after writing
// Send the content to the propay server
writer.println();

unnecessary and a possible reason why it's not working
writer.println(content);
writer.println();

unnecessary and a possible reason why it's not working
writer.flush();

String line = reader.readLine();
while( line != null ) {
System.out.println( line );
line = reader.readLine();
}
}

<?xml version="1.0" standalone="no"?>
<XMLResponse xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.propay.com/schema/PPResponse.xsd">
</XMLResponse>

Any ideas on why the java.net version of the code does not act the
same?

It should act the same, but keep in mind that the
HttpUrlConnection is keeping away everything protocol-
specific, e.g. the need of setting Content-Length if
posting and the empty line between request-header
and -body.

Google for doing POST with HttpUrlConnection. There
is a load of examples out there how it's working.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top