Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中使用HttpWebResponse从webApi获取数据_C#_Json_Httpwebrequest_Encapsulation - Fatal编程技术网

C# 在C中使用HttpWebResponse从webApi获取数据

C# 在C中使用HttpWebResponse从webApi获取数据,c#,json,httpwebrequest,encapsulation,C#,Json,Httpwebrequest,Encapsulation,我正在尝试从上的web应用程序获取数据 这是一个风险计算器,我必须向它发送一些数据,并通过HttpWebRequest获得响应。我启动了一个会话并恢复了cookies。现在我正试图向它发布数据,但没有收到任何响应。这是我的密码 var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://apps.theocc.com/encore2/home.do"); httpWebRequest.Method = "

我正在尝试从上的web应用程序获取数据 这是一个风险计算器,我必须向它发送一些数据,并通过HttpWebRequest获得响应。我启动了一个会话并恢复了cookies。现在我正试图向它发布数据,但没有收到任何响应。这是我的密码

    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();
我发布的文件的格式必须正确,因为这是我从同一个web应用程序手动获取的文件

会话恢复方法就这么简单

    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;
    }
我做错了什么?
任何建议都会有帮助。

您能发布您期望的响应示例吗?{maxPositionsLimit:0,isInvalidFilename:false,done:true,data:{},PositionSlimiteExceeded:false,success:true}