从c#中的Request.BufferedInputStream读取100行(< = 100个字符),花费将近6秒

时间:2015-04-19 18:09:45

标签: c# asp.net

这里我正在尝试从请求体中读取字符串行。每行包含sms消息的数据。单行转换为单个sms对象。我已经测量了在localhost中作为请求主体的100个数据行的发布请求的时间。但这需要将近6秒钟。我希望它应该快很多。我有什么遗漏。

Stopwatch stcc3 = new Stopwatch();
stcc3.Start();
string[] lines = body();
var list = convert(lines);
stcc3.Stop();
Console.WriteLine("time3: " + stcc3.Elapsed);

这是两个函数的主体

    private string[] body()
    {

        using (Stream inp = Request.GetBufferedInputStream())
        {
            using (StreamReader reader = new StreamReader(inp, Encoding.UTF8))
            {
                List<String> lines = new List<string>();
                while(!reader.EndOfStream)
                {
                    lines.Add(reader.ReadLine());
                }
                return lines.ToArray();
            }
        }
    }

    private List<ISms> convert(string[] lines)
    {
        List<ISms> list = new List<ISms>();
        for (int idx = 0; idx < lines.Length; idx++)
        {
            string ln = lines[idx];
            ISms iSms = new ISms();
            string[] wd = ln.Split(new char[] { '|' });
            int i = 0;
            iSms.id = parseInt(wd[i++]);
            iSms.messageDate = DateTime.Parse(wd[i++]);
            iSms.receiveDate = DateTime.Parse(wd[i++]);
            iSms.originator = wd[i++];
            iSms.text = wd[i++];
            list.Add(iSms);
        }
        return list;
    }

这是ISms课程。

public class ISms {
    public Int64 id;
    public String originator;
    public DateTime messageDate;
    public DateTime receiveDate;
    public String text;
    public string status = "undefined";
    public String description;
    public Int64 contact_id;

    public ISms() {
    }

    public ISms(String originator, DateTime messageDate, DateTime receiveDate, String text, String description) {
        this.originator = originator;
        this.messageDate = messageDate;
        this.receiveDate = receiveDate;
        this.text = text;
        this.description = description;
    }

    public override String ToString() {
        return "originator: " + originator + ", messageDate: " + messageDate + ", text: " + text;
    }
}

我正在从java程序发出post请求。我使用spring RestTemplate向上面的c#mvc控制器发出post请求。这是代码

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add(HttpHeaders.AUTHORIZATION, authToken);
        interceptHeaders(httpHeaders);
        OSms[] oSmsArray = rt.postForObject(getPostUrl(), new HttpEntity(convert(queue), httpHeaders), OSms[].class);
        onSuccess(queue);
        onSuccess(oSmsArray);

这是转换功能的主体

public static String convert(List<ISms> queue) {
    StringBuilder sb = new StringBuilder();
    queue.forEach((sms) -> {
        sb
                .append(sms.getId())
                .append(seperator)
                .append(convert(sms.getMessageDate()))
                .append(seperator)
                .append(convert(sms.getReceiveDate()))
                .append(seperator)
                .append(sms.getOriginator())
                .append(seperator)
                .append(sms.getText())
                .append('\n');
    });
    return sb.toString();
}

0 个答案:

没有答案