C# を使用して Java アプレットを自動化するために、サーブレット呼び出しを呼び出す必要があります。Java アプレットとは、URL 接続オブジェクトを使用してサーブレットを呼び出します。
URL servlet = new URL(servletProtocol, servletHost, servletPort, "/" + ServletName);
URLConnection con = servlet.openConnection();
con.setDoOutput(true);
ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream());
// Write several parameters strings
out.writeObject(param[0]);
out.writeObject(param[1]);
out.flush();
out.close();
問題は、c# を使用してこれをシミュレートする必要があることです。対応するオブジェクトは HttpWebRequest になると思います
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(servletPath);
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(param[0],0,param[0].length);
newStream.Write(param[1],0,param[1].length);
newStream.Close();
文字列をシリアル化された Java 文字列として書き込むにはどうすればよいですか? ここに回避策はありますか?Java の ObjectOutputStream のドキュメントによると、プリミティブ型以外のオブジェクトをシリアル化します。私は文字列がクラスであることを知っているので、オブジェクトまたは特別なケースのようにシリアライズしますか?
IKVM (http://www.ikvm.net/) Java 仮想マシンをリファレンスにインポートし、Java で java.io ライブラリを使用しようとしています。残念ながら、ObjectInputStream コンストラクターが呼び出されると、「無効なストリーム ヘッダー」がスローされます。
これが私の変更されたコードです:
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
List<byte> lByte = new List<byte>();
using (StreamReader sr = new StreamReader(myRequest.GetResponse().GetResponseStream()))
{
while (sr.Peek() >= 0)
{
lByte.Add((byte)sr.Read());
}
}
byte[] bArr = lByte.ToArray();
ObjectInputStream inputStream = null;
try
{
//Construct the ObjectInputStream object
inputStream = new ObjectInputStream(new ByteArrayInputStream(bArr));
Object obj = null;
while ((obj = inputStream.readObject()) != null)
{
string objStr = obj as string;
}
}
catch (java.lang.Exception ex)