post方式 可以兼容<CC>,get方式不能兼容空格、符号, soap方式PC端经常使用,但是作为移动端则每次交互时数据量比较大,仅包头就320字节,
soap方式还需要base64转码。
//回执
private static String getReceiptCCData() {
String recpstr = "<CC> <CU> <AssessRecpID = "+ PushMsgInfo.getRecpID() + " RecvR = 1/> </CU> </CC>";
byte[] assess = Base64.encode(recpstr.getBytes(), Base64.DEFAULT);
String str = new String(assess);
Log.d(TAG, "recpstr = " + recpstr);
Log.d(TAG, "assess = " + str);
return str;
}
//post方式
private static String sendProcessRetReceipt() {String strResult = "";
String OFFLINEMSG_URL = "http://192.168.10.109/msgdevxcst/processretreceipt.aspx";
HttpPost httpRequest = new HttpPost(OFFLINEMSG_URL);
String key = JJMD5.getMD5(imeiData + uidData + verisonData + msgTypeData +
ccAssessData + "JJMSGDX");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("szIMEI_IN", imeiData));
params.add(new BasicNameValuePair("dwUid_IN", Long.toString(uidData)));
params.add(new BasicNameValuePair("szCltVerison_IN", verisonData));
params.add(new BasicNameValuePair("dwMsgType_IN", Integer.toString(msgTypeData)));
params.add(new BasicNameValuePair("stCC_IN", ccAssessData));
params.add(new BasicNameValuePair("szParamCheck_IN", key));
try {
HttpResponse httpResponse;
//发出HTTP request
httpRequest.setEntity((HttpEntity) new UrlEncodedFormEntity(params,HTTP.UTF_8));
//取得HTTP response
httpResponse = new DefaultHttpClient().execute(httpRequest);
//若状态码为200
if(httpResponse.getStatusLine().getStatusCode()==200){
//取出回应字串
strResult=EntityUtils.toString(httpResponse.getEntity());
}
Log.d(TAG, "strResult = " + strResult);
Log.d(TAG, "strResult 4 = " + httpResponse.getStatusLine().getStatusCode());
} catch (Exception e) {
Log.i(TAG, "Pose Failed!!!");
e.printStackTrace();
}
return strResult;
}
//get 方式
private static String sendProcessRetReceipt() {
String OFFLINEMSG_URL = "http://192.168.10.109/msgdevxcst/processretreceipt.aspx";
PushByteBuffer bb = new PushByteBuffer();
InputStream content = null;
StringBuffer url = new StringBuffer(OFFLINEMSG_URL + "?");
String key = JJMD5.getMD5(imeiData + uidData + verisonData + msgTypeData +
ccAssessData + "JJMSGDX");
String uri = url.toString() + "szIMEI_IN=" + imeiData + "&dwUid_IN=" + uidData +
"&szCltVerison_IN=" + verisonData + "&dwMsgType_IN=" + msgTypeData +
"&stCC_IN=" + ccAssessData + "&szParamCheck_IN=" + key;
try {
Log.d(TAG, "msgTypeData 1 = " + uri);
HttpGet httpGet = new HttpGet(uri);
Log.d(TAG, "msgTypeData 2 = " + uri);
HttpClient httpclient = new DefaultHttpClient();
HttpParams params = httpclient.getParams();
if (params != null) {
params.setIntParameter(HttpConnectionParams.SO_TIMEOUT, 15000);
params.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 15000);
}
Log.d(TAG, "msgTypeData 3 = " + params);
HttpResponse response = httpclient.execute(httpGet);
content = response.getEntity().getContent();
Log.d(TAG, "msgTypeData 4 = " + content);
if (content != null) {
byte[] bt = new byte[1024];
int i = 0;
while ((i = content.read(bt)) > 0) {
bb.addBytes(bt, 0, i);
}
content.close();
}
Log.d(TAG, "sendRoarOfflineCloud, final=" + new String(bb.getBytes(), Charset.forName("Utf-8").name()));
return new String(bb.getBytes(), Charset.forName("Utf-8").name());
} catch (Exception e) {
return null;
}
}
//soap方式
/**
* getSoapProcessRetReceipt
*
*/
public static String getSoapProcessRetReceipt() {
try {
String soapstr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
+ " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
+ "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Body><ProcessRetReceipt xmlns=\"http://msgdevxcst.webservice.jj.cn/msgdevxcst/\">"
+ "<szIMEI_IN>658256789012345</szIMEI_IN>"
+ "<dwUid_IN>156246900</dwUid_IN>"
+ "<szCltVerison_IN>V40203</szCltVerison_IN>"
+ "<dwMsgType_IN>501</dwMsgType_IN>"
+ "<stCC_IN>PENDPjxDVT48QXNzZXNzIFJlYWRSID0gMSBSZWNwSUQgPSAxMiAvPjwvQ1U+PC9DQz4=</stCC_IN>"
+ "<szParamCheck_IN>0E92CD541D750097D6539965FAB86AEA</szParamCheck_IN>"
+ "</ProcessRetReceipt>"
+ "</soap:Body>"
+ "</soap:Envelope>";
// http://192.168.18.168/msgdevxcst/TKWmsgdevxcstService.asmx
// http://192.168.10.109/msgdevxcst/tkwmsgdevxcstservice.asmx
String soap = toSoapString(false);
Log.d(TAG, "Receipt soap = " + soap);
Log.d(TAG, "Receipt soap length = " + soap.length());
URL url = new URL(
"http://192.168.10.109/msgdevxcst/tkwmsgdevxcstservice.asmx?wsdl");
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", Integer.toString(soap
.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction",
"http://msgdevxcst.webservice.jj.cn/msgdevxcst/ProcessRetReceipt ");
OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
osw.write(soap);
osw.flush();
osw.close();
StringBuilder sTotalString = new StringBuilder();
String sCurrentLine = "";
InputStream is = conn.getInputStream();
BufferedReader l_reader = new BufferedReader(new InputStreamReader(is));
while ((sCurrentLine = l_reader.readLine()) != null) {
sTotalString.append(sCurrentLine);
}
return sTotalString.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
本文详细介绍了在不同场景下使用POST、GET和SOAP方式发送接收CC数据的方法,包括编码、转码及处理响应的过程。

被折叠的 条评论
为什么被折叠?



