txclahnydx
封建炎月
2016-02-24 12:58

Java开发支付宝接口(报文报不是UTF-8编码的错误)

  • 支付宝
  • java
  • 编码
  • utf-8

package com.spasvo.main;

import com.csii.pp.key.KeyProvider;
import com.csii.pp.signature.XMLSignature;
import com.csii.pp.transport.HttpClientTransport;

import java.net.URL;

public class AlipayTest {

private XMLSignature xs = new com.csii.pp.signature.XMLSignature();

private static String contentType = "application/xml; charset=utf-8";

private static String encoding = "UTF-8";

   //签名
public String sign(String xmlString, String sigNode) throws Exception{

    byte[] signature = xs.sign(xmlString.getBytes("UTF-8"), "Message", sigNode, kp.getPrivateKey("bkt0452010061201", "111111"));

    return new String(signature, "UTF-8");
}

// public void send(String urlString,String xmlString,String reqId){
public void send(){
String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>3.0.0ALIPAYALIPAY200707250020140610 10:21:52王洪章6217524512001000319D123010219441024241613502513211";
String reqId = "CSVReq";
String urlString = "http://130.1.9.222:9082/paygate/alipay";
try{
HttpClientTransport httpClientTransport = new HttpClientTransport();

        String body=sign(xmlString, reqId);

        StringBuffer message = new StringBuffer(messageHead);

        message.append(body);
        String all = message.toString();

        System.out.println("请求报文:"+all);

        URL url = new URL(urlString);
        httpClientTransport.setProtocol(url.getProtocol());
        httpClientTransport.setHost(url.getHost());
        httpClientTransport.setPort(url.getPort() == -1 ? url.getDefaultPort() : url.getPort());
        httpClientTransport.setTarget(url.getPath());
        httpClientTransport.setQueryString(url.getQuery());
        httpClientTransport.setContentType(contentType);
    httpClientTransport.submit(all.getBytes());
    }catch(Exception e){
        e.printStackTrace();
    }

}
public static void main(String[] args) {
    AlipayTest a = new AlipayTest();

    try {
        a.send();

// a.send(url,xmlString,reqId);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这个是测试的程序,可以成功发送和接受报文,但是用项目调用这个方法发送报文的时候,给我们返回的报文报我们发送的报文不是UTF-8编码的报文。我的项目的编码是UTF-8的编码,类的编码也是UTF-8的编码, 求大神指教一下这是什么原因。

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答