私がしようとしているのは、クライアントアプリケーションとasp.netページの間でシリアル化されたデータを交換することです。
は私が交換のために次のクラスを使用します。
public class Send
{
public Guid guidField;
public string stringField1;
public string stringField2;
public byte[] data;
}
と、クライアント側で
public class Receive
{
public Guid guidField;
public byte[] data;
}
私が要求するために、次のコードを使用します。
public Receive Exchange(Send send)
{
Receive receive = new Receive();
string address = "example.org";
HttpWebRequest client = (HttpWebRequest)WebRequest.Create(address);
client.ContentType = "application/x-www-form-urlencoded";
client.Timeout = 90000;
client.Method = "POST";
client.UserAgent = "AgentMe";
try
{
Stream stream = client.GetRequestStream();
PackSend(stream, send);
stream.Flush();
stream.Close();
var response = client.GetResponse();
Stream outputStream = response.GetResponseStream();
UnpackReceive(outputStream, out receive);
}
catch (WebException ex)
{ }
return receive;
}
サーバー側を私は同様に反対の方向に行う:
梱包と私はNewtonsoft.Jsonを使用し、データをアンパックするためprotected void Page_Load(object sender, EventArgs e)
{
Stream inputStream = Request.InputStream;
Send send;
UnpackSend(inputStream, out send);
// here goes some useful work
Receive receive = Process(send);
Response.ContentType = "application/x-www-form-urlencoded";
Stream stream = Response.OutputStream;
PackReceive(stream, sent);
Response.End();
}
:
static void PackSend(Stream stream, Send message)
{
BsonWriter writer = new BsonWriter(stream);
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(writer, message);
writer.Flush();
writer.Close();
}
void UnpackSend(Stream stream, out Send message)
{
BsonReader reader = new BsonReader(stream);
JsonSerializer serializer = new JsonSerializer();
message = serializer.Deserialize<Send>(reader);
}
PackReceive/UnpackReceiveのためのコードは似ています。
私はContentType = "application/x-www-form-urlencoded"
を使用していますが、交換することはできますが、public byte[] data
フィールドサイズが〜1200バイトを超えないようにしてください。サイズが大きい場合は、リクエストを行っているInternal Serverエラー500が表示されます。
ContentType = "text/xml"
を使用。 「パブリックバイト[]データ」フィールドの任意のサイズは、サーバー側で適切に処理されます。しかし、サーバが応答ストリームに書き込もうとすると、エラーが発生し、自動的にリクエストが繰り返され、クライアントアプリケーションが停止し、サーバに何らかのエラーを発生させずに複数の同様の要求が氾濫します。 ContentType = "application/octet-stream"
- "text/xml"と同じ動作を示します。
誰でも適切なContentType
文字列を示唆したり、この状況を適切に処理する方法をアドバイスしてください。ありがとうございました。