Solr URL从webclient查询字符串POST发出错误请求(400)

时间:2018-10-03 18:48:58

标签: c# solr webclient

在将查询字符串中的多个fq传递给SOLR时,我遇到了奇怪的问题。     一个fq可以正常工作,但是多个fq参数不起作用。     我在这个问题上花了两天时间:(     根据MSDN,多个fq转换为fq = 1,2,3     我写了实用程序来拆分逗号,现在fq看起来像     fq = 1&fq = 2&fq = 3     我从SOLR UI尝试了同样的方法,效果很好。     示例-

https://example.com:8765/solr/test/select?fq=last_save_date:{2016-01-01T00:00:00Z TO 2018-01-01T23:59:59Z}&fq=id:*.pdf&q="John"&start=0&rows=20

我从我的C#代码中尝试了同样的方法,它的URL形式为-

https://example.com:8765/solr/test/select?fq=last_save_date:{2016-01-01T00:00:00Z TO 2018-01-01T23:59:59Z}&fq=id:*.pdf&q="John"&start=0&rows=20

有趣的是,当我直接在浏览器中尝试该网址时,我会从代码中获得响应,即使我尝试下面的代码来检查网址是否正确并返回预期结果-

    string html;
 using (WebClient client = new WebClient())
 {
 var string1 = "\"John\"";
 html = client.DownloadString(@"https://example.com:3456/solr/test/select?fq=last_save_date:{2016-01-01T00:00:00Z TO 2018-01-01T23:59:59Z}&fq=id:*.pdf&q=" + string1 + "&start=0&rows=20");
                    }

我的代码是-

System.Uri uri = new System.Uri(solrCoreConnection + "/example");

                SolrUtility utility = new SolrUtility("fq"); // This utility split commas from multiple fq and add &fq=
                using (WebClient wc = new WebClient())
                {

                    if (!string.IsNullOrEmpty(dateTime))
                    {                        
                         utility.Add("fq", dateTime);
                    }
            if (!string.IsNullOrEmpty(filename))
                    {                        
                         utility.Add("fq", filename);
                    }
            wc.QueryString = utility;
// This line throws bad request (400)
                    byte[] data = wc.UploadValues(uri, "POST", wc.QueryString); 

         catch (WebException ex)
                    {
                if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
                {
                    var resp = (HttpWebResponse)ex.Response;
                    if (resp.StatusCode == HttpStatusCode.NotFound) // HTTP 404
                    {
                        //Handle it
                    }
                }
                //Handle it
            }

0 个答案:

没有答案