2
private string getLngLat(string strLAC, string strCID)
    {
        string str;
        try
        {
            HttpWebRequest length = (HttpWebRequest)WebRequest.Create(new Uri("http://www.google.com/glm/mmap"));
            length.Method = "POST";
            int num = Convert.ToInt32(strLAC);
            byte[] numArray = AjaxFrm.PostData(num, Convert.ToInt32(strCID));
            length.ContentLength = (long)((int)numArray.Length);
            length.ContentType = "application/binary";
            length.GetRequestStream().Write(numArray, 0, (int)numArray.Length);
            HttpWebResponse response = (HttpWebResponse)length.GetResponse();
            byte[] numArray1 = new byte[checked((IntPtr)response.ContentLength)]; // ERROR AT HERE
            response.GetResponseStream().Read(numArray1, 0, (int)numArray1.Length);
            if (response.StatusCode != HttpStatusCode.OK)
            {
                str = string.Format("Error {0}", response.StatusCode);
            }
            else
            {
                byte num1 = numArray1[0];
                byte num2 = numArray1[1];
                byte num3 = numArray1[2];
                if ((numArray1[3] << 24 | numArray1[4] << 16 | numArray1[5] << 8 | numArray1[6]) != 0)
                {
                    str = "";
                }
                else
                {
                    double num4 = (double)(numArray1[7] << 24 | numArray1[8] << 16 | numArray1[9] << 8 | numArray1[10]) / 1000000;
                    double num5 = (double)(numArray1[11] << 24 | numArray1[12] << 16 | numArray1[13] << 8 | numArray1[14]) / 1000000;
                    str = string.Format("{0}&{1}", num5, num4);
                }
            }
        }
        catch (Exception exception)
        {
            str = string.Format("Error {0}", exception.Message);
        }
        return str;
    }

byte[] numArray1 = new byte[checked((IntPtr)response.ContentLength)];

ここでエラーが発生します

Error 3 Cannot implicitly convert type 'System.IntPtr' to 'int'. An explicit conversion exists (are you missing a cast?)

なぜこのエラーが発生するのでしょうか? それを解決する方法は?

4

2 に答える 2

0

に変換することが目的の場合intは、代わりにそれを行う必要があります。

byte[] numArray1 = new byte[checked((int)response.ContentLength)];

これにより、 に 2GB 以上を割り当てようとするのを避けることができますnumArray1ただし、コンパイラの観点からこれを行う必要はありませんlong。長さの値を使用して配列を割り当てることができます。サイズが十分に大きい場合、実行時にフォールオーバーするだけです。(.NET 4.5 では、アプリケーション構成設定を使用して、合計サイズが 2GB を超える配列を許可できますが、要素数がを超えることを許可するかどうかはわかりませんint.MaxValue。)

したがって、実際には、チェックを CLR に任せたほうがよいでしょう。

byte[] numArray1 = new byte[response.ContentLength];

...または、最大サイズを課したい場合は、明示的に行います(私も最大2GB未満をお勧めします...)

ContentLengthまた、クライアントが応答ヘッダーで指定していないことを示す -1 である可能性も考慮する必要があります。

于 2013-08-14T06:16:21.863 に答える