使用C#中的HttpWebResponse从Web Api获取数据

时间:2015-02-02 09:20:04

标签: c# json httpwebrequest encapsulation

我正试图通过http://apps.theocc.com/encore2/home.do上的网络应用获取数据 这是一个风险计算器,我必须向它发送一些数据并通过C#HttpWebRequest获得响应。我开始了一个会话并恢复了cookie。现在我正在尝试向其发布数据,但我没有收到任何回复。这是我的代码

    var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://apps.theocc.com/encore2/home.do");

        httpWebRequest.Method = "GET";
        httpWebRequest.KeepAlive = true;

        var cookieContainer = new CookieContainer();
        httpWebRequest.CookieContainer = cookieContainer;

        WebHeaderCollection headerCollection = new WebHeaderCollection();

        using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
        {
            /* save headers */
            for (int i = 0; i < response.Headers.Count; i++)
            {
                headerCollection.Add(response.Headers.AllKeys[i], response.Headers.Get(i));
            }
            /* save cookies */
            foreach (Cookie cookie in response.Cookies)
            {
                cookieContainer.Add(cookie);
            }
        }

        httpWebRequest = (HttpWebRequest)WebRequest.Create("http://apps.theocc.com/pmc/pmcdUploadFile.json");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        httpWebRequest.KeepAlive = true;

        //Just restoring header and setting back cookies
        util.RestoreSession(ref httpWebRequest,cookieContainer,headerCollection);

        var sb = new StringBuilder();

        var boundary = "----WebKitFormBoundaryKWYPlRmSNoLdzrb7";

        sb.AppendFormat(boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"positionFile\"; filename=\"positions.csv\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Type:  application/vnd.ms-excel");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Media Type: application/vnd.ms-excel");
        sb.AppendFormat("\r\n");
        sb.AppendFormat(boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("form-data: form-data; name=\"refresh\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        using (FileStream fs = new FileStream(@"C:\Users\....\positions.csv", FileMode.Open, FileAccess.Read))
        {
            byte[] contents = new byte[fs.Length];
            fs.Read(contents, 0, contents.Length);
            sb.Append(Encoding.Default.GetString(contents));
        }
        sb.AppendFormat("\r\n");
        sb.AppendFormat(boundary);
        sb.AppendFormat("\r\n");

        byte[] fulldata = Encoding.Default.GetBytes(sb.ToString());
        httpWebRequest.ContentLength = fulldata.Length;
        using (Stream sw = httpWebRequest.GetRequestStream())
        {
            sw.Write(fulldata, 0, fulldata.Length);
        }
       var resp = (HttpWebResponse)httpWebRequest.GetResponse();

我发布的文件格式必须正常,因为这是我手动从同一个网络应用程序获得的文件。

会话恢复方法就像这个

一样简单
    public static void RestoreSession(HttpWebRequest webRqst, CookieContainer cc, WebHeaderCollection hc)
    {
        /* restore Session ID */
        for (int i = 0; i < hc.Count; i++)
        {
            string key = hc.GetKey(i);
            if (key == "Set-Cookie")
            {
                key = "Cookie";
            }
            else
            {
                continue;
            }
            string value = hc.Get(i);
            webRqst.Headers.Add(key, value);
        }
        /* restore Cookies */
        webRqst.CookieContainer = cc;
    }

我在这里做错了什么? 任何建议都会有所帮助。

0 个答案:

没有答案