Pregunta

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

here i get the error with

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

How come will get this error? how to solve it?

¿Fue útil?

Solución

Just specify the array length directly, there's no need to attempt to convert to IntPtr first:

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

IntPtr is a type only intended to hold pointer values, so the usual rules for integral types don't apply. A pointer value is not a sensible array length, so the language designers did not permit using IntPtr to specify an array length.

Otros consejos

If your aim was to convert to an int, you just need to do that instead:

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

That will avoid you trying to allocate more than 2GB for numArray1. You don't need to do this from a compiler perspective though - you can allocate an array using a long value for the length. It's just that it will fall over at execution time if the size is large enough. (In .NET 4.5 you can use an application configuration setting to allow for arrays with a total size of more than 2GB, but I'm not sure offhand whether that allows for the number of elements to exceed int.MaxValue.)

So actually, you'd be better off with just leaving the checking to the CLR:

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

... or if you do want to impose some maximum size, do that explicitly. (I'd suggest a maximum of less than 2GB, too...)

You should also consider the possibility of ContentLength being -1, indicating that the client hasn't specified it in a response header.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top