I have the activity below in my android application that creates a httpsurlconnection with authentication. It then posts some XML and captures the response. The response I receive from the server is that no XML message has been received but there are no exceptions in the log.
I have tested the same code as a Java application and it works fine. I have searched high and low for a couple of weeks but cannot seem to find an answer.
I have set the android.permission.INTERNET in my manifest file.
Any help will be much appreciated, I have posted my code below minus the username and password used in the connection.
Activity:
package com.payments.WorldPay;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class OrderModification extends Activity
{
TextView resultMessage;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.order_modification);
Button submit = (Button)findViewById(R.id.submitButton);
submit.setOnClickListener(submitModification);
resultMessage = (TextView)this.findViewById(R.id.result);
}
OnClickListener submitModification = new OnClickListener()
{
public void onClick(View v)
{
StringBuffer xmlMod = new StringBuffer();
xmlMod.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xmlMod.append("<!DOCTYPE paymentService PUBLIC \"-//RBS WorldPay/DTD RBS WorldPay
PaymentService v1//EN\" \"http://dtd.wp3.rbsworldpay.com/paymentService_v1.dtd\">");
xmlMod.append("<paymentService version=\"1.4\" merchantCode=\"TECHSUPPORT\">");
xmlMod.append("<modify>");
xmlMod.append("<orderModification orderCode=\"123456789\">");
xmlMod.append("<cancel/>");
xmlMod.append("</orderModification>");
xmlMod.append("</modify>");
xmlMod.append("</paymentService>");
SendXml sendMod = new SendXml(xmlMod.toString());
resultMessage.setText(sendMod.submitOrder());
}
};
}
Connection Class:
package com.payments.WorldPay;
import java.io.*;
import java.net.*;
import javax.net.ssl.HttpsURLConnection;
public class SendXml
{
protected final static String merchantCode="username";
protected final static String xmlPassword="password";
protected final static String envUrl="https://secure-test.wp3.rbsworldpay.com/jsp/merchant/xml/paymentService.jsp";
protected String xmlRequest;
public SendXml(String xmlRequest)
{
this.xmlRequest = xmlRequest;
}
public String submitOrder()
{
HttpsURLConnection conn = null;
try
{
System.setProperty("http.keepAlive", "false");
Authenticator.setDefault(new MyAuthenticator());
URL url = new URL(envUrl);
conn = (HttpsURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setAllowUserInt开发者_JAVA技巧eraction(false);
conn.setConnectTimeout(30000);
String utf8Xml = URLEncoder.encode(xmlRequest, "UTF-8");
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(utf8Xml);
wr.flush();
wr.close();
InputStream response;
if(conn.getResponseCode()==200)
{
response = conn.getInputStream();
}
else
{
response = conn.getErrorStream();
}
BufferedReader in = new BufferedReader(new InputStreamReader(response),4800);
StringBuffer responseBuffer = new StringBuffer();
String line;
while ((line = in.readLine()) != null)
{
responseBuffer.append(line);
}
in.close();
return responseBuffer.toString();
}
catch(Exception e)
{
return("Connection Error: "+e);
}
finally
{
conn.disconnect();
}
}
public static class MyAuthenticator extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return (new PasswordAuthentication(merchantCode, xmlPassword.toCharArray()));
}
}
}
I have run the code again as a Java project and it works fine. I have included example request and response below.
Request:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE paymentService PUBLIC "-//RBS WorldPay/DTD RBS WorldPay PaymentService v1//EN"
"http://dtd.wp3.rbsworldpay.com/paymentService_v1.dtd">
<paymentService version="1.4" merchantCode="TECHSUPPORT">
<modify>
<orderModification orderCode="123456789">
<cancel/>
</orderModification>
</modify>
</paymentService>
Response:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE paymentService PUBLIC "-//Bibit//DTD Bibit PaymentService v1//EN"
"http://dtd.bibit.com/paymentService_v1.dtd">
<paymentService version="1.4" merchantCode="TECHSUPPORT">
<reply>
<ok>
<cancelReceived orderCode="123456789"/>
</ok>
</reply>
</paymentService>
If I simply remove the > from the element above I will receive the error below.
Error Response:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE paymentService PUBLIC "-//Bibit//DTD Bibit PaymentService v1//EN"
"http://dtd.bibit.com/paymentService_v1.dtd">
<paymentService version="1.4" merchantCode="TECHSUPPORT">
<reply>
<error code="2">
<![CDATA[Element type "modify" must be followed by either attribute specifications, ">" or
"/>".]]>
</error>
</reply>
</paymentService>
Thanks in advance Darren
I didn't find an answer to my question but the code below allowed me to successfully post my XML.
public ArrayList<PaymentResult> submitOrder()
{
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(envUrl);
UsernamePasswordCredentials login = new UsernamePasswordCredentials(merchantCode, xmlPassword);
StringBuffer responseBuffer = null;
try
{
request.addHeader(BasicScheme.authenticate(login,"UTF-8",false));
StringEntity xmlmessage = new StringEntity(xmlRequest,HTTP.UTF_8);
xmlmessage.setContentType("text/xml");
request.setHeader("Content-Type","text/xml;charset=UTF-8");
request.setEntity(xmlmessage);
HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
responseBuffer = new StringBuffer();
String line;
while ((line = in.readLine()) != null)
{
responseBuffer.append(line);
}
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return parseResponse(responseBuffer.toString());
}
精彩评论