2011-05-27 87 views
1

我在我的android应用程序中创建一个带有身份验证的httpsurlconnection下面的活动。然后它会发布一些XML并捕获响应。我从服务器收到的响应是没有收到XML消息,但日志中没有例外。Android的HttpsURLConnection发布问题

我已经测试了与Java应用程序相同的代码,它工作正常。我搜索了几个星期的高和低,但似乎无法找到答案。

我在我的清单文件中设置了android.permission.INTERNET。

任何帮助将不胜感激,我已经发布了我的代码,减去连接中使用的用户名和密码。

活动:

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

连接类:

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

我再次运行该代码的Java项目,它工作正常。我在下面列出了示例请求和响应。

请求:

<?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> 

响应:

<?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> 

如果我简单地从元件去除>上述我会得到以下错误。

错误响应:

<?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> 

在此先感谢 达伦

+1

嗨达伦 - 请缩进你的代码,这很难读取它的方式。 – MByD 2011-05-27 18:55:27

+0

如果你不UrlEncode你的xmlRequest会发生什么? – 2011-05-27 18:56:19

+0

对不起MByD的第一篇文章。 – darren 2011-05-28 11:53:08

回答

3

我没有找到一个回答我的问题,但下面的代码让我成功后我的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()); 
}