using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Diagnostics;/*
* Sample code to obtain geo codes from a cell info
* "GSM/UMTS" setting revealed by smuraro, thanks!
*/namespace GMM {
    class Program {
        static byte[] PostData(int MCC, int MNC, int LAC, int CID, bool shortCID) {
            /* The shortCID parameter follows heuristic experiences:
             * Sometimes UMTS CIDs are build up from the original GSM CID (lower 4 hex digits)
             * and the RNC-ID left shifted into the upper 4 digits.
             */
            byte[] pd = new byte[] {
                0x00, 0x0e,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00,
                0x00, 0x00,
                0x00, 0x00,                0x1b,
                0x00, 0x00, 0x00, 0x00, // Offset 0x11
                0x00, 0x00, 0x00, 0x00, // Offset 0x15
                0x00, 0x00, 0x00, 0x00, // Offset 0x19
                0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, // Offset 0x1f
                0x00, 0x00, 0x00, 0x00, // Offset 0x23
                0x00, 0x00, 0x00, 0x00, // Offset 0x27
                0x00, 0x00, 0x00, 0x00, // Offset 0x2b
                0xff, 0xff, 0xff, 0xff,
                0x00, 0x00, 0x00, 0x00
            };            bool isUMTSCell = ((Int64)CID > 65535);            if (isUMTSCell)
                Console.WriteLine("UMTS CID. {0}", shortCID ? "Using short CID to resolve." : "");
            else
                Console.WriteLine("GSM CID given.");            if (shortCID)
                CID &= 0xFFFF;      /* Attempt to resolve the cell using the GSM CID part */            if ((Int64)CID > 65536) /* GSM: 4 hex digits, UTMS: 6 hex digits */
                pd[0x1c] = 5;
            else
                pd[0x1c] = 3;            pd[0x11] = (byte)((MNC >> 24) & 0xFF);
            pd[0x12] = (byte)((MNC >> 16) & 0xFF);
            pd[0x13] = (byte)((MNC >> 8) & 0xFF);
            pd[0x14] = (byte)((MNC >> 0) & 0xFF);            pd[0x15] = (byte)((MCC >> 24) & 0xFF);
            pd[0x16] = (byte)((MCC >> 16) & 0xFF);
            pd[0x17] = (byte)((MCC >> 8) & 0xFF);
            pd[0x18] = (byte)((MCC >> 0) & 0xFF);            pd[0x27] = (byte)((MNC >> 24) & 0xFF);
            pd[0x28] = (byte)((MNC >> 16) & 0xFF);
            pd[0x29] = (byte)((MNC >> 8) & 0xFF);
            pd[0x2a] = (byte)((MNC >> 0) & 0xFF);            pd[0x2b] = (byte)((MCC >> 24) & 0xFF);
            pd[0x2c] = (byte)((MCC >> 16) & 0xFF);
            pd[0x2d] = (byte)((MCC >> 8) & 0xFF);
            pd[0x2e] = (byte)((MCC >> 0) & 0xFF);            pd[0x1f] = (byte)((CID >> 24) & 0xFF);
            pd[0x20] = (byte)((CID >> 16) & 0xFF);
            pd[0x21] = (byte)((CID >> 8) & 0xFF);
            pd[0x22] = (byte)((CID >> 0) & 0xFF);            pd[0x23] = (byte)((LAC >> 24) & 0xFF);
            pd[0x24] = (byte)((LAC >> 16) & 0xFF);
            pd[0x25] = (byte)((LAC >> 8) & 0xFF);
            pd[0x26] = (byte)((LAC >> 0) & 0xFF);            return pd;
        }        static void Main(string[] args) {            if (args.Length < 4) {
                Console.WriteLine("Usage: gmm MCC MNC LAC CID [\"shortcid\"]");
                return;
            }
            string shortCID = "";   /* Default, no change at all */
            if (args.Length == 5)   
                 shortCID = args[4].ToLower();            try {
                String url = "http://www.google.com/glm/mmap";
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(url));
                req.Method = "POST";
               
                int MCC = Convert.ToInt32(args[0]);
                int MNC = Convert.ToInt32(args[1]);
                int LAC = Convert.ToInt32(args[2]);
                int CID = Convert.ToInt32(args[3]);
                byte[] pd = PostData(MCC, MNC, LAC, CID, shortCID == "shortcid");                req.ContentLength = pd.Length;
                req.ContentType = "application/binary";
                Stream outputStream = req.GetRequestStream();
                outputStream.Write(pd, 0, pd.Length);
                outputStream.Close();                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                byte[] ps = new byte[res.ContentLength];
                int totalBytesRead = 0;
                while (totalBytesRead < ps.Length) {
                    totalBytesRead += res.GetResponseStream().Read(ps, totalBytesRead, ps.Length - totalBytesRead);
                }                if (res.StatusCode == HttpStatusCode.OK) {
                    short opcode1 = (short)(ps[0] << 8 | ps[1]);
                    byte opcode2 = ps[2];
                    System.Diagnostics.Debug.Assert(opcode1 == 0x0e);
                    System.Diagnostics.Debug.Assert(opcode2 == 0x1b);
                    int ret_code = (int)((ps[3] << 24) | (ps[4] << 16) | (ps[5] << 8) | (ps[6]));
                       
                    if (ret_code == 0) {
                        double lat = ((double)((ps[7] << 24) | (ps[8] << 16) | (ps[9] << 8) | (ps[10]))) / 1000000;
                        double lon = ((double)((ps[11] << 24) | (ps[12] << 16) | (ps[13] << 8) | (ps[14]))) / 1000000;
                        Console.WriteLine("Latitude: {0}, Longitude: {1}", lat, lon);
                     
                        Process p = new Process();
                        p.StartInfo.FileName = "iexplore";                        Console.WriteLine("\nClose map window to exit\n");                        p.StartInfo.Arguments = String.Format(
                            "http://maps.google.de/maps?f=q&hl=de&q={0},{1}&ie=UTF8&z=15",
                            lat.ToString().Replace(',','.'), lon.ToString().Replace(',','.'));
                        p.Start();
                        p.WaitForExit();
                    }
                    else
                        Console.WriteLine("Error {0}", ret_code);
                }
                else
                    Console.WriteLine("HTTP Status {0} {1}", res.StatusCode, res.StatusDescription);
            }
            catch (Exception) {
                throw;
            }
        }
    }
}

解决方案 »

  1.   

    上面是错的..
    using System;using System.Collections.Generic;using System.Text;using System.Net;using System.IO;namespace GeoCodeFromCell {
        class Program {
            static byte[] PostData(int LAC, int CID) {
                byte[] pd = new byte[] {
                    0x00,0x15, // function code?
                    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  // Sessionid??
                    0x00,0x02,0x66,0x72,  // country code
                    0x00,0x12,0x53,0x6f,0x6e,0x79,0x5f,0x45,0x72,0x69,0x63,0x73,0x73,0x6f,0x6e,0x2d,0x4b,0x37,0x35,0x30, //user agent
                    0x00,0x05,0x31,0x2e,0x33,0x2e,0x31, // version
                    0x00,0x03,0x57,0x65,0x62, // "Web"
                    0x1b,  // op code?
                    0x00,0x00,0x00,0x00, // ??
                    0x00,0x00,0x00,0x00, // ??
                    0x00,0x00,0x00,0x03, // ??
                    0x00,0x00, // ??
                    0x00,0x00,0xe3,0xbf,    // CID
                    0x00,0x00,0x31,0x1b,    // LAC
                    0x00,0x00,0x00,0x00, // ??
                    0x00,0x00,0x00,0x00, // ??
                    0x00,0x00,0x00,0x00, // ??
                    0x00,0x00,0x00,0x00 // ??
                };
                pd[0x3d] = (byte)((CID >> 24) & 0xFF);
                pd[0x3e] = (byte)((CID >> 16) & 0xFF);
                pd[0x3f] = (byte)((CID >> 8) & 0xFF);
                pd[0x40] = (byte)((CID >> 0) & 0xFF);
                pd[0x41] = (byte)((LAC >> 24) & 0xFF);
                pd[0x42] = (byte)((LAC >> 16) & 0xFF);
                pd[0x43] = (byte)((LAC >> 8) & 0xFF);
                pd[0x44] = (byte)((LAC >> 0) & 0xFF);
                return pd;
            }
            static void Main(string[] args) {
                if (args.Length != 2) {
                    Console.WriteLine("Usage: GeoCodeFromCell LAC CID");
                    return;
                }
                try {
                    String url = "http://www.google.com/glm/mmap";;
                    HttpWebRequest req =(HttpWebRequest)WebRequest.Create(new Uri(url));
                    req.Method = "POST";
                    int LAC = Convert.ToInt32(args[0]);
                    int CID = Convert.ToInt32(args[1]);
                    byte[] pd = PostData(LAC, CID);
                    req.ContentLength = pd.Length;
                    req.ContentType = "application/binary";
                    req.GetRequestStream().Write(pd, 0, pd.Length);
                    HttpWebResponse res =(HttpWebResponse)req.GetResponse();
                    byte[] ps = new byte[res.ContentLength];
                    res.GetResponseStream().Read(ps, 0, ps.Length);
                    Console.WriteLine("LAC {0}, CID {1}", LAC, CID);
                    if (res.StatusCode == HttpStatusCode.OK) {
                        short code = (short)(ps[0] << 8 | ps[1]);
                        byte opcode = ps[2];
                        System.Diagnostics.Debug.Assert(code == 0x15);
                        System.Diagnostics.Debug.Assert(opcode == 27);
                        int ret_code = (int)((ps[3] << 24) | (ps[4] << 16)| (ps[5] << 8) | (ps[6]));
                        if (ret_code == 0) {
                            double lat = ((double)((ps[7] << 24) | (ps[8]<< 16) | (ps[9] << 8) | (ps[10]))) / 1000000;
                            double lon = ((double)((ps[11] << 24) |(ps[12] << 16) | (ps[13] << 8) | (ps[14]))) / 1000000;
                            Console.WriteLine("Latitude: {0}, Longitude:{1}", lat, lon);
                        }
                        else
                            Console.WriteLine("Error {0}", ret_code);
                    }
                    else
                        Console.WriteLine("HTTP Status {0} {1}",res.StatusCode, res.StatusDescription);            }
                catch (Exception) {
                    throw;
                }        }
        }

      

  2.   

    我觉得你若对C#和java的语法很熟且知道两者的异同点,自己解决就行了,人家改了,你若还不了解java语法,没什么意义啊