Exemple #1
0
        /// 获取坐标城市信息
        /// </summary>
        /// <param name="location"></param>
        /// <param name="errortimes"></param>
        /// <returns></returns>
        public static string SelectLatLngCity(string location, int errortimes)
        {
            string city = null;

            try
            {
                Prolongation.JHWebClient client = new Prolongation.JHWebClient();

                System.Threading.Thread.Sleep(100);
                string   locationinfo = client.DownloadString("http://maps.google.com/maps/api/geocode/json?sensor=false&address=" + location).Replace(" ", "");
                string[] a            = locationinfo.Split('\n');
                if (locationinfo.IndexOf("\"status\":\"OK\"") != -1 && locationinfo.IndexOf("\"administrative_area_level") != -1)
                {
                    for (int i = 0; i < a.Length; i++)
                    {
                        if (a[i].IndexOf("\"administrative_area_level") != -1)
                        {
                            city = new Regex("\"long_name\":\"(.*?)\"").Matches(a[i - 2])[0].Groups[1].ToString();
                            break;
                        }
                    }
                }
            }
            catch (Exception)
            {
                if (errortimes < 3)
                {
                    city = SelectLatLngCity(location, errortimes + 1);
                }
            }

            return(city);
        }
Exemple #2
0
        /// <summary>
        /// 获取指定位置经纬度
        /// </summary>
        /// <param name="samelocation"></param>
        /// <param name="errortimes">0</param>
        /// <returns></returns>
        public static SameLocation GetLatLng(SameLocation samelocation, int errortimes)
        {
            try
            {
                Prolongation.JHWebClient client = new Prolongation.JHWebClient();

                System.Threading.Thread.Sleep(100);
                string locationinfo = client.DownloadString("http://maps.google.com/maps/api/geocode/json?sensor=false&address=" + samelocation.Location);
                if (locationinfo.IndexOf("\"location\"") != -1)
                {
                    string LatLng = new Regex("\"location\":{(.*?)},").Matches(locationinfo.Replace(" ", "").Replace("\n", ""))[0].Groups[1].ToString();
                    string Lat    = LatLng.Split(',')[0].Replace("\"lat\":", "");
                    string Lng    = LatLng.Split(',')[1].Replace("\"lng\":", "");
                    samelocation.Lat = Lat;
                    samelocation.Lng = Lng;
                }
                else
                {
                    if (errortimes < 3)
                    {
                        samelocation = GetLatLng(samelocation, errortimes + 1);
                    }
                    else
                    {
                        samelocation.Lat = "DataError";
                    }
                }
            }
            catch (Exception)
            {
                if (errortimes < 3)
                {
                    samelocation = GetLatLng(samelocation, errortimes + 1);
                }
                else
                {
                    samelocation.Lat = "NetworkError";
                }
            }

            return(samelocation);
        }
Exemple #3
0
        /// <summary>
        /// 获取File
        /// </summary>
        /// <param name="file"></param>
        /// <param name="format"></param>
        public static bool GetFile(string parentfile, string file, string format)
        {
            try
            {
                Prolongation.JHWebClient client = new Prolongation.JHWebClient();

                client.Headers.Add("Content-Type: multipart/form-data; boundary=-------------------------idiot");

                string poststring = "---------------------------idiot\r\n"
                                    + "Content-Disposition: form-data; name=\"db\"\r\n\r\n"
                                    + "nuccore\r\n"
                                    + "---------------------------idiot\r\n"
                                    + "Content-Disposition: form-data; name=\"file\"; filename=\"1.txt\"\r\n"
                                    + "Content-Type: text/plain\r\n\r\n";

                foreach (string version in File.ReadAllLines(parentfile, Encoding.Default))
                {
                    poststring += version + "\r\n";
                }

                poststring += "\r\n" + "---------------------------idiot--\r\n";

                byte[] response;

                try
                {
                    response = client.UploadData("http://www.ncbi.nlm.nih.gov/portal/utils/batchentrez_p.cgi", "POST", Encoding.UTF8.GetBytes(poststring));
                }
                catch (Exception a)
                {
                    try
                    {
                        System.Threading.Thread.Sleep(100);
                        response = client.UploadData("http://www.ncbi.nlm.nih.gov/portal/utils/batchentrez_p.cgi", "POST", Encoding.UTF8.GetBytes(poststring));
                    }
                    catch (Exception e)
                    {
                        return(false);
                    }
                }

                string responsestring = Encoding.UTF8.GetString(response);
                string key            = new Regex("QueryKey=(.*?)\"").Matches(responsestring.Replace(" ", "").Replace("\n", ""))[0].Groups[1].ToString();
                string qty            = new Regex("for(.*?)UID").Matches(responsestring.Replace(" ", "").Replace("\n", ""))[0].Groups[1].ToString();

                for (int i = 0; i < 5; i++)
                {
                    try
                    {
                        client.DownloadString("http://www.ncbi.nlm.nih.gov/sites/entrez?db=nuccore&cmd=HistorySearch&QueryKey=" + key);
                        break;
                    }
                    catch (Exception)
                    {
                        System.Threading.Thread.Sleep(100);
                        if (i == 4)
                        {
                            return(false);
                        }
                        ;
                    }
                }

                //格式筛选
                switch (format)
                {
                case "GenBank":
                    for (int i = 0; i < 5; i++)
                    {
                        try
                        {
                            client.DownloadFile("http://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&sendto=on&log$=seqview&db=nuccore&dopt=genbank&sort=&query_key=" + key + "&qty=" + qty + "&filter=all", file + ".gb");
                            break;
                        }
                        catch (Exception)
                        {
                            System.Threading.Thread.Sleep(100);
                            if (i == 4)
                            {
                                return(false);
                            }
                            ;
                        }
                    }
                    break;

                case "GenBank(Full)":
                    for (int i = 0; i < 5; i++)
                    {
                        try
                        {
                            client.DownloadFile("http://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&sendto=on&log$=seqview&db=nuccore&dopt=gbwithparts&sort=&query_key=" + key + "&qty=" + qty + "&filter=all", file + ".gb");
                            break;
                        }
                        catch (Exception)
                        {
                            System.Threading.Thread.Sleep(100);
                            if (i == 4)
                            {
                                return(false);
                            }
                            ;
                        }
                    }
                    break;

                case "FASTA":
                    for (int i = 0; i < 5; i++)
                    {
                        try
                        {
                            client.DownloadFile("http://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&sendto=on&log$=seqview&db=nuccore&dopt=fasta&sort=&query_key=" + key + "&qty=" + qty + "&filter=all", file + ".fasta");
                            break;
                        }
                        catch (Exception e)
                        {
                            string a = e.ToString();
                            System.Threading.Thread.Sleep(100);
                            if (i == 4)
                            {
                                return(false);
                            }
                            ;
                        }
                    }
                    break;

                case "XML":
                    for (int i = 0; i < 5; i++)
                    {
                        try
                        {
                            client.DownloadFile("http://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&sendto=on&log$=seqview&db=nuccore&dopt=xml&sort=&query_key=" + key + "&qty=" + qty + "&filter=all", file + ".xml");
                            break;
                        }
                        catch (Exception)
                        {
                            System.Threading.Thread.Sleep(100);
                            if (i == 4)
                            {
                                return(false);
                            }
                            ;
                        }
                    }
                    break;

                case "INSDSeq XML":
                    for (int i = 0; i < 5; i++)
                    {
                        try
                        {
                            client.DownloadFile("http://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&sendto=on&log$=seqview&db=nuccore&dopt=gbc_xml&sort=&query_key=" + key + "&qty=" + qty + "&filter=all", file + ".gbc.xml");
                            break;
                        }
                        catch (Exception)
                        {
                            System.Threading.Thread.Sleep(100);
                            if (i == 4)
                            {
                                return(false);
                            }
                            ;
                        }
                    }
                    break;

                case "TinySeq XML":
                    for (int i = 0; i < 5; i++)
                    {
                        try
                        {
                            client.DownloadFile("http://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&sendto=on&log$=seqview&db=nuccore&dopt=fasta_xml&sort=&query_key=" + key + "&qty=" + qty + "&filter=all", file + ".fasta.xml");
                            break;
                        }
                        catch (Exception)
                        {
                            System.Threading.Thread.Sleep(100);
                            if (i == 4)
                            {
                                return(false);
                            }
                            ;
                        }
                    }
                    break;

                case "Feature Table":
                    for (int i = 0; i < 5; i++)
                    {
                        try
                        {
                            client.DownloadFile("http://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&sendto=on&log$=seqview&db=nuccore&dopt=ft&sort=&query_key=" + key + "&qty=" + qty + "&filter=all", file + ".txt");
                            break;
                        }
                        catch (Exception)
                        {
                            System.Threading.Thread.Sleep(100);
                            if (i == 4)
                            {
                                return(false);
                            }
                            ;
                        }
                    }
                    break;

                case "GI List":
                    for (int i = 0; i < 5; i++)
                    {
                        try
                        {
                            client.DownloadFile("http://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&sendto=on&log$=seqview&db=nuccore&dopt=gilist&sort=&query_key=" + key + "&qty=" + qty + "&filter=all", file + ".txt");
                            break;
                        }
                        catch (Exception)
                        {
                            System.Threading.Thread.Sleep(100);
                            if (i == 4)
                            {
                                return(false);
                            }
                            ;
                        }
                    }
                    break;
                }
                return(true);
            }
            catch (Exception e)
            {
                string b = e.ToString();
                return(false);
            }
        }