none
MultiformDataContent fails on PCL for System.Net.Http when using names that contain brackets (e.g. mynamedvalue[foo]) RRS feed

  • Question

  • I'm using the System.Net.Http PCL library on iOs/Android with Xamarin.  The distinction is necessary as this code works just fine with the non PCL version.   I'm simply trying to add content to the body of an HttpRequest, but an exception is thrown.   The exception is only thrown (for my case) when the name contains [] (brackets), though it probably fails on other extended characters.

    The exception I get is:

    Invalid format.  at System.Net.Http.Headers.NameValueHeaderValue.set_Value (System.String value)

    I think it's pretty straightforward in that it's choking on the bracket as when I remove the bracket, it works just fine.   I tried encoding it  (e.g. mynamedvalue%5Bfoo%5Da) but unfortunately, that was not the trick (though it did post successfully, it came through the with encoding in the name.  I also tried string literals, and a string literal with escape [ but these yielded the same exception.

    I suspect this is probably just a parsing error in the PCL, however, if it is not and someone has an idea where I might be going wrong, I'd sure appreciate it.

    Or, if anyone knows of a good workaround, that would be fantastic as well.

    I thought I had a workaround setting the MultiFormData part myself via serialization, but what kind of object would I use?  I tried many dictionary's of strings, objects and httpcontents which successfully posts but the name isn't there (e.g. Dictionary<string,HttpContent> data = new Dictionary<string,HttpContent >();                

    Sunday, September 14, 2014 11:13 PM

Answers

All replies

  • Hello patrickdengler,

    >> I suspect this is probably just a parsing error in the PCL, however, if it is not and someone has an idea where I might be going wrong, I'd sure appreciate it.

    Inside this class, it has a check for the input string, characters as [,],” ”, are not supported. You coudl try to replace the [/] with a valid character. As you said, this “mynamedvalue%5Bfoo%5Da” did post successfully, so the “%” should be valid. You could use it as:

    try
    
                {
    
                    string value = @"Name%Value%";
    
                    NameValueHeaderValue result = new NameValueHeaderValue(value);
    
                }
    
                catch (Exception)
    
                {
    
                }
    

    If I misunderstand, please let me know.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 15, 2014 3:48 AM
    Moderator
  • Thanks so much for responding. 

    If I understand your solution, I think you are suggesting I change the [] to a valid character.  Unfortunately, I do not control the server, and the server expects the [] in the name.  Was that your suggestion?

    Monday, September 15, 2014 12:29 PM
  • Or perhaps better worded, if the library is checking for brackets, and the server requires brackets, how can I work around this?

    Dexter

    Monday, September 15, 2014 3:55 PM
  • Hello patrickdengler,

    >> Was that your suggestion?

    Yes, you understand correctly.

    >> Or perhaps better worded, if the library is checking for brackets, and the server requires brackets, how can I work around this?

    Maybe you could try with the HttpHeaders.AddWithoutValidation Method which adds the specified header and its the value into the HttpHeaders collection without validating the provided information.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 16, 2014 8:21 AM
    Moderator
  • I went that route unfortunately that method is not available in the PCL version (and these are not headers, they are fields, which I am not sure makes a difference?)
    Tuesday, September 16, 2014 9:19 PM
  • Hello patrickdengler,

    Ok, it seems that the System.Net.Http PCL library is not work for this scenario, I would suggest that you post a voice to:

    https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/3158400-add-httpclient-support-in-portable-class-libraries

    To let team release a PCL version which could support specific character as “[,],’ ’”.

    Bets Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 17, 2014 7:49 AM
    Moderator
  • Thanks Fred.  And I didn't answer your question about trying the Headers code you gave me.  The result is:

    "  at System.Net.Http.Headers.Parser+Token.Check (System.String s) [0x00028] in /Developer/MonoTouch/Source/monotouch/_build//Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.Net.Http/System.Net.Http.Headers/Parser.cs:59 \n  at System.Net.Http.Headers.NameValueHeaderValue..ctor (System.String name, System.String value) [0x00006] in /Developer/MonoTouch/Source/monotouch/_build//Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.Net.Http/System.Net.Http.Headers/NameValueHeaderValue.cs:44 \n  at System.Net.Http.Headers.NameValueHeaderValue..ctor (System.String name) [0x00000] in /Developer/MonoTouch/Source/monotouch/_build//Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.Net.Http/System.Net.Http.Headers/NameValueHeaderValue.cs:38 \n  at MediaPickerSample.NativeUploader.GetDescriptionFromImage (System.String path) [0x00034] in c:\\Users\\patd\\Documents\\Xamarin.Mobile-master\\Xamarin.Mobile-master\\MonoTouch\\Samples\\MediaPickerSample\\NativeUploader.cs:56 "

    Wednesday, September 17, 2014 4:50 PM
  • I may have found a workaround, but I am a little stuck still.  I'm using the following code but I get WebRequest was cancelled.  Most of the posts say that the cheap trick is to turn KeepAlive off, but I think that flag was dropped (as it is not available) and is defaulted to on.

        

    varreq = System.Net.HttpWebRequest.Create(newUri(CamFindUrl));

      

               

                req.ContentType =

    "multipart/form-data";//; boundary=" + boundary;


                req.ContentLength = stream.Length;

                req.Timeout = 500000;

                req.Method =

    "Post";

               

    StreamWritermyWriter = new


                

    StreamWriter(req.GetRequestStream());

                myWriter.Write(stream);

                myWriter.Close();

               

    varresp = req.GetResponse();

    Wednesday, September 17, 2014 6:44 PM
  • Hi Patrick

    This was a problem with the mono implementation used in Xamarin Android (it has been fixed but has not reached stable branch yet)

    I have posted a solution here:

    http://stackoverflow.com/questions/21824775/system-net-http-httpclient-instance-throws-an-exception-when-setting-a-comma-wh/29145584#29145584

    Peter


    • Edited by PeterO_dk Thursday, March 19, 2015 1:17 PM added link
    Thursday, March 19, 2015 1:17 PM