背景是这样,现有一个JAVA的程序,运行正常能够得到返回值
代码如下
代码如下
package Demo; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpClientParams; import com.google.gson.Gson; public class Demo { public void login() { String url = "http://116.228.73.38:2227/receive//plogin.action"; String js = "000367{"tid":"LYSTO2760000001","trace":"","mty":"0851","opOrgCode":"900000","deviceType":"PDA","opTerminal":"LYSTO2760000001","empCode":"9000000615","pdaPassword":"1234","verifyDate":"2016-06-20 09:40:08","connectionType":"2","os":"01","osVersion":"0.0.0.0","softVersion":"0.0.0.0","pdaType":"LYSTO"}"; doPostClient(js, url); } public static String doPostClient(String json, String url) { HttpClient httpClient = new HttpClient(); String rval = ""; PostMethod postMethod = new PostMethod(url); try { Gson gson = new Gson(); InputStream in = new ByteArrayInputStream(objectToByte(json)); postMethod.setRequestBody(in); HttpClientParams params = new HttpClientParams(); httpClient.setParams(params); httpClient.executeMethod(postMethod); byte[] b = postMethod.getResponseBody(); System.out.println(b.length); String rtnData = (String) byteToObject(b); rval = gson.toJson(rtnData); System.out.println(rtnData); } catch (Exception e) { System.out.println(e.getMessage() + "," + e.getStackTrace()); } finally { postMethod.releaseConnection(); } return rval; } public static byte[] objectToByte(java.lang.Object obj) { byte[] bytes = null; ObjectOutputStream oo = null; try { ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(out); gzip.write(obj.toString().getBytes("utf-8")); gzip.close(); bytes = out.toByteArray(); } catch (Exception e) { System.out.println("translation" + e.getMessage()); e.printStackTrace(); } finally { if (oo != null) { try { oo.close(); } catch (IOException e) { e.printStackTrace(); } } } return bytes; } private static java.lang.Object byteToObject(byte[] bytes) { java.lang.Object obj = null; ObjectInputStream oi = null; try { ByteArrayInputStream bi = new ByteArrayInputStream(bytes); GZIPInputStream gzipi = new GZIPInputStream(bi); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gzipi, "UTF-8")); String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } catch (Exception e) { System.out.println("translation" + e.getMessage()); e.printStackTrace(); } finally { if (oi != null) { try { oi.close(); } catch (IOException e) { e.printStackTrace(); } } } return obj; } public static void main(String[] args) { Demo client = new Demo(); client.login(); } }
本人转换成的C#程序
public static void LogIN()
{
String url = "http://116.228.73.38:2227/receive/plogin.action";
String js = "000367{"tid":"LYSTO2760000001","trace":"","mty":"0851","opOrgCode":"900000","deviceType":"PDA","opTerminal":"LYSTO2760000001","empCode":"9000000615","pdaPassword":"1234","verifyDate":"2016-06-20 09:40:08","connectionType":"2","os":"01","osVersion":"0.0.0.0","softVersion":"0.0.0.0","pdaType":"LYSTO"}";
DoPostClient(js, url);
}
public static string DoPostClient(string json, string url)
{
HttpWebRequest req = null;
HttpWebResponse rsp = null;
System.IO.Stream reqStream = null;
try
{
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] postData = Compress(json);
req.Proxy = null;
req.ContentLength = postData.Length;
reqStream = req.GetRequestStream();
reqStream.Write(postData, 0, postData.Length);
rsp = (HttpWebResponse)req.GetResponse();
string resut = "";
return resut;
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
if (reqStream != null) reqStream.Close();
if (rsp != null) rsp.Close();
}
}
/// <summary>
/// 压缩字符串
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static byte[] Compress(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
using (MemoryStream ms = new MemoryStream())
{
GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, false);
compressedzipStream.Write(buffer, 0, buffer.Length);
compressedzipStream.Close();
buffer = ms.ToArray();
}
int[] test = new int[buffer.Length];
for(int i=0;i<buffer.Length;i++)
{
if(buffer[i]>127)
test[i] = buffer[i] - 256;
else
test[i] = buffer[i];
}
return buffer; ;
}
原因是C#和JAVA的Byte不一样,java的可以,而C#的不成功,求高手帮解答,C#返回404,JAVA程序传入的参数假如不进行 GZIP压缩服务器也会无应答, 大家有什么方法让本人C#端把 那个Java的程序调通吗,本人试了转化JAVA为c#的DLL,也没成功,HttpClient初始化总失败,看看谁能在C#中把那段Java程序运行起来
解决方案
10
byte[] rawData = System.Text.Encoding.UTF8.GetBytes(text); MemoryStream ms = new MemoryStream(); GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true); compressedzipStream.Write(rawData, 0, rawData.Length); compressedzipStream.Close(); buffer =ms.ToArray();
试试。 或换下编码格式。
30
本人写的测试代码:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var bytes = Compress("TEST");
Console.WriteLine(Convert.ToBase64String(bytes)); //H4sIAAAAAAAEAAtxDQ4BALiT6u4EAAAA
}
public static byte[] Compress(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
using (MemoryStream ms = new MemoryStream())
{
GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, false);
compressedzipStream.Write(buffer, 0, buffer.Length);
compressedzipStream.Close();
buffer = ms.ToArray();
}
int[] test = new int[buffer.Length];
for (int i = 0; i < buffer.Length; i++)
{
if (buffer[i] > 127)
test[i] = buffer[i] - 256;
else
test[i] = buffer[i];
}
return buffer; ;
}
}
public class Main {
public static void main(String[] args) {
byte[] bytes = objectToByte("TEST");
String str = (new sun.misc.BASE64Encoder()).encode(bytes);
System.out.println(str); //H4sIAAAAAAAAAAtxDQ4BALiT6u4EAAAA
}
public static byte[] objectToByte(java.lang.Object obj)
{
byte[] bytes = null;
ObjectOutputStream oo = null;
try
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(obj.toString().getBytes("utf-8"));
gzip.close();
bytes = out.toByteArray();
} catch (Exception e)
{
System.out.println("translation" + e.getMessage());
e.printStackTrace();
} finally
{
if (oo != null)
{
try
{
oo.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
return bytes;
}
}
你确定有对比过C# Gzip压缩和java Gzip压缩后的字节数组能否一致?
60
你本人吧java里的gzip方法编译成jar包,甚至把整段代码都封装到jar里,提供给C#就可以了