与asp页面交换BSON数据

发布于 2024-10-10 18:14:06 字数 2580 浏览 0 评论 0原文

再会。

我想做的是在客户端应用程序和 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;
}

在服务器端,我执行类似的操作,但方向相反:

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();
}

对于打包和解包数据,我使用 Newtonsoft.Json :

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字节。如果大小较大,我会收到内部服务器错误 500 执行请求。

使用 ContentType = "text/xml";任何大小的“公共字节[]数据”字段都可以在服务器端正确处理。有用的工作已经完成,但是当服务器尝试写入响应流时,我猜会发生错误并自动重复请求,因此客户端应用程序被卡住,用多个类似的请求淹没服务器而不会引发任何错误。 ContentType = "application/octet-stream" - 显示与“text/xml”相同的行为。

任何人都可以建议一个正确的 ContentType 字符串或提供如何正确处理这种情况的建议。谢谢。

Good day.

What am I trying to do is to exchange serialized data between client application and asp.net page.

I use the following classes for exchange:

public class Send
{
    public Guid guidField;
    public string stringField1;
    public string stringField2;
    public byte[] data;
}

and

public class Receive
{
    public Guid guidField;
    public byte[] data;
}

On client side I use the following code to make a request:

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;
}

On server side I do it similarly but in the opposite direction:

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();
}

For packing and unpacking data I use Newtonsoft.Json :

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);
}

The code for PackReceive/UnpackReceive is analogous.

When I use ContentType = "application/x-www-form-urlencoded" I'm able to do exchange but if only public byte[] data field size doesn't exceed ~1200 bytes. If size is larger I get Internal Server error 500 doing request.

Using ContentType = "text/xml"; any size of "public byte[] data" field is handled properly on server side. Useful work is done but when server tries to write into response stream I guess error occurs and automagicly request is repeated so client application is stuck, flooding server with multiple similar requests without throwing any error.
ContentType = "application/octet-stream" - shows the same behavior as "text/xml".

Can anyone suggest a proper ContentType string or give an advice how to proper handle this situation. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断桥再见 2024-10-17 18:14:06

您的 500 错误消息是通用 IIS 错误消息而不是 ASP.Net 错误消息吗?如果是这样,则可能意味着数据甚至无法到达 ASP.Net,并且 IIS 正在被数据阻塞。我的第一个猜测是您遇到了上传限制。 以下是在 IIS 6 中增强此功能的方法。设置 IIS 后,您还必须 更改 ASP.Net 的值。至于你的其他错误,你应该真正考虑捕获它们。 .Net 框架很好地解释了问题所在。

Is your 500 error message the generic IIS one and not the ASP.Net one? If so that probably means that the data isn't even getting to ASP.Net and that IIS is choking on it. My first guess is that you're running into an upload limit. Here's a way to increase this in IIS 6. Once IIS is set you will also have to change the value for ASP.Net. As for your other errors, you should really look into trapping them. The .Net framework does a pretty good job of explaining what goes wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文