DWQA QuestionsCategory: ProgramThrow an exception at system. Net. Http. Headers. Httpheaders. Contains (string name)
Ocean asked 4 months ago

Throw an exception at system. Net. Http. Headers. Httpheaders. Checkheadername (string name) at system. Net. Http. Headers. Httpheaders. Contains (string name)
The details are as follows:

public static HttpClientResult httpPost(string url, IDictionary<string, string> headMap, IDictionary<string, string> @params, string filePath, ProgressNotifier progressNotifier)
        {
            string response = "";
            String starttime = datetime. Now. ToString ("DD / MMM / yyyy: HH: mm: SS ZZ", new cultureinfo ("en US")); // request time
            string endTime = "-";
            string statusCode = "-";
            string contentLength = "-";
            string contentType = "-";

            try
            {
                HttpClient hc = new HttpClient();
                hc.Timeout = new TimeSpan(1000000000);
                HttpResponseMessage responseM = null;

                //Set request header
                if (headMap != null && headMap.Count > 0)
                {
                    foreach (string name in headMap.Keys)
                    {
                        hc.DefaultRequestHeaders.TryAddWithoutValidation(name, headMap[name]);
                    }
                }

                //Set request body
                var paramsList = new List<KeyValuePair<string, string>>();

                if (filePath != null)
                {
                    //MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create();    
                    //Discard
                    //BandwithLimiterFileBody fileBody = new BandwithLimiterFileBody(file, null, "application/octet-stream", null, BaseBlockUtil.maxRate, progressNotifier);
                    //mEntityBuilder.addPart("file", fileBody);
                    //mEntityBuilder.addTextBody("desc", file.Name);

                    var fileContent = new ByteArrayContent(File.ReadAllBytes(filePath));
                    string fileName = Path.GetFileName(filePath);
                    var stringContent = new StringContent(fileName, Encoding.UTF8);
                    var content = new MultipartFormDataContent();
                    content.Add(fileContent, "file", fileName);
                    content.Add(stringContent, "desc");


                    if (@params != null && @params.Count > 0)
                    {
                        foreach (string name in @params.Keys)
                        {
                            //mEntityBuilder.addTextBody(name, @params[name], ContentType.create("text/plain", Charset.forName("UTF-8")));
                            //How to set MIME type in todo
                            content.Add(new StringContent(@params[name], Encoding.UTF8), name);
                        }
                    }

                    responseM = hc.PostAsync(new Uri(url), content).Result;
                }
                else if (@params != null && @params.Count > 0)
                {
                    foreach (string name in @params.Keys)
                    {
                        paramsList.Add(new KeyValuePair<string, string>(name, @params[name]));
                    }
                    //How does todo set the character set httpentity he = new urlencoded formentity (paramslist, "UTF-8") here;
                    var content = new FormUrlEncodedContent(paramsList);
                    responseM = hc.PostAsync(new Uri(url), content).Result;
                }

                endTime = DateTime.Now.ToString("dd/MMM/yyyy:HH:mm:ss zz", new CultureInfo("en-US"));

                //Get response headers information
                HttpResponseHeaders headers = responseM.Headers;
                IEnumerable<string> values;
                if (headers.Contains("Content-Length"))
                {
                    values = headers.GetValues("Content - Length");
                    IEnumerator i = values.GetEnumerator();
                    if (i.MoveNext())
                    {
                        contentLength = i.Current.ToString();
                    }
                }
                //TODO to check
                if (headers.Contains("Content-Type"))
                {
                    values = headers.GetValues("Content-Type");
                    IEnumerator i = values.GetEnumerator();
                    if (i.MoveNext())
                    {
                        // ReSharper disable once RedundantAssignment
                        contentType = i.Current.ToString();
                    }
                }

                //Get HTTP response body (content)
                int status;
                response = GetResponse(out status, responseM);

                if (status == 200)
                {
                    if (!(new JsonValidator()).validate(response))
                    {
                        response = EncodeUtils.urlsafeDecodeString(response);
                    }
                }
                return new HttpClientResult(status, response);
            }
            catch (Exception e)
            {
                statusCode = "500";
                endTime = DateTime.Now.ToString("dd/MMM/yyyy:HH:mm:ss zz", new CultureInfo("en-US"));
                throw new Exception(e.ToString());
            }
            finally
            {
                //writeHttpLog(startTime, url, "-", (null != @params ? @params["token"] : "-"), (null != file ? file.Name : "-"), "-", "-", endTime, statusCode, contentType, contentLength, response);
            }
        }

Throw an exception at system. Net. Http. Headers. Httpheaders. Contains (string name)
Exception thrown in red mark part:
Throw an exception at system. Net. Http. Headers. Httpheaders. Contains (string name)

System.Exception: System.InvalidOperationException: Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

   In system. Net. Http. Headers. Httpheaders. Checkheadername (string name)

   In system. Net. Http. Headers. Httpheaders. Contains (string name)

   At com.chinanetcenter.api.util.httclientutil.httppost (string URL, idictionary ` 2 headmap, idictionary ` 2 params, string filepath, progressnotifier progressnotifier), C: \ codes \ SVN \ WCS sharp SDK \ WCS CSharp SDK \ main \ util \ httclientutil. CS: Line 127
    Httppost (string URL, idictionary ` 2 headmap, idictionary ` 2 params, string filepath, progressnotifier, progressnotifier) httpclientutil.cs: Line 155
   Httclientutil.cs: line 35 at com.chinanetcenter.api.util.httppost (string URL, idictionary ` 2 headmap, idictionary ` 2 params, string filepath)
   Fileuploadcommand.cs: Line 164 at com.chinanetcenter.api.wsbox.fileuploadcommand.upload (string bucket name, string filekey, string srcfile, putpolicy putpolicy, progresslistener)
   Fileuploadcommand.cs: Line 207 at com.chinanetcenter.api.wsbox.fileuploadcommand.upload (string bucket name, string filekey, string srcfile, putpolicy putpolicy)
   In unittest. Fileuploadcommandtest. Uploadtest2() location fileuploadcommandtest. CS: Line 152