M
M
maksustoff2017-04-11 08:03:48
In contact with
maksustoff, 2017-04-11 08:03:48

The string is too long. How to pass a large array to a VK API stored procedure?

The task is to check among a large number of ID's whether there are those who have common friends with the ID being checked. The method friends.getMutualallows you to check such a connection with no more than 100 IDs in one call. In order to speed up this business, I created a stored procedure that should receive 2500 IDs and check for common ones in 25 requests. The problem is how to transfer these 2500 IDs to a stored procedure. If you insert a comma into the link (to get it through Args), an exception "string too large" is generated. Is there any way to get around this fact or will I have to check 100 each? Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tom Nolane, 2017-04-11
@maksustoff

this is how i send via post

// сам post
public async Task<string> POST(string url, NameValueCollection nvc = null)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = WebRequestMethods.Http.Post;
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0";
                request.AllowAutoRedirect = true;
                request.ProtocolVersion = HttpVersion.Version11;
                request.AllowWriteStreamBuffering = true;
                request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
                request.ContentType = "multipart/form-data; boundary=" + boundary; 
                request.Headers.Add("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3");
                request.Headers.Add("Accept-Encoding", "gzip, deflate");
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 

                if (nvc != null)
                {
                    Stream rs = request.GetRequestStream();

                    byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
                    string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
                    string _key = String.Empty;
                    foreach (string key in nvc.Keys)
                    {
                        rs.Write(boundarybytes, 0, boundarybytes.Length);
                        string formitem = string.Format(formdataTemplate, key, nvc[key]);
                        byte[] formitembytes = Encoding.UTF8.GetBytes(formitem);
                        rs.Write(formitembytes, 0, formitembytes.Length);
                    }
                    rs.Write(boundarybytes, 0, boundarybytes.Length);
                } 

                return await Task.Run(() =>
                {
                    string resp = RESPONSE(request);
                    return resp;
                });
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
//  post обращается к  ответу
 public string RESPONSE(HttpWebRequest request)
        {
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string answer = "";
                var headers = response.Headers.ToString();

                if (Convert.ToInt32(response.StatusCode) == 302 || Convert.ToInt32(response.StatusCode) == 200)
                {
                    using (Stream rspStm = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(rspStm, Encoding.UTF8, true))
                        {
                            answer = String.Empty; answer = reader.ReadToEnd();
                        }
                    } 
                    return answer;
                }
                else
                {
                    response.Close(); return WebUtility.HtmlDecode(response.StatusDescription);
                }
            }
            catch (Exception ex)
            {
                return WebUtility.HtmlDecode(ex.Message);
            }
        }

//после этого сам  код: (на примере wall.get)
public async System.Threading.Tasks.Task<bool> GetWall(int id_group, int offset = 0)
        {
            try
            {
                string url = "https://api.vk.com/method/wall.get?";
                Main_Tools mt = new Main_Tools();

                // вот что вам надо: создать коллекцию и передать в post (и добавлять/заполнять через for || foreach для вашего случая

                NameValueCollection nvc = new NameValueCollection();
                nvc.Add("access_token", VK_main.TOKEN);
                nvc.Add("owner_id", id_group.ToString());
                nvc.Add("count", "1");
                if(offset != 0) nvc.Add("offset", offset.ToString());
                nvc.Add("filter", "owner"); 
                nvc.Add("v", VK_main.VERSION);
                
                // добавляете сколько вам нужно параметров и передаёте в метод POST вторым аргументом

                string t = await mt.POST(url, nvc);
               dynamic d = JObject.Parse(t);
               // и далее разбираете json
//.. и т.д.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question