send utf-8 string http request

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mullin Yu

    send utf-8 string http request

    I want to submit a utf-8 xml request to a servlet by the following coding.
    it seesm that the servlet can't recognize it correctly.

    can i just using string postData = "..... utf-8 data" and then save the
    files at utf-8 encoded at the vs.net ide?

    or if i use string, vs.net ide will conside it to be utf-16???

    UTF8Encoding encoding = new UTF8Encoding();


    string postData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +

    "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"urn:schem as-xmlsoap-org:envelope\"> " +

    "<SOAP-ENV:Header>" +

    "<SOAPActionVer sion>1.5.5</SOAPActionVersi on></SOAP-ENV:Header>" +

    "<SOAP-ENV:Body>" +

    "<cmEDMSCustome rContactPage transactionType =\"UPDT\">" +

    "<cmEDMSCustome rContactPageSer vice>" +

    "<cmEDMSCustome rContactPageHea der PageReadSW=\"fa lse\"
    CustomerContact ID=\"8210710493 \"></cmEDMSCustomerC ontactPageHeade r>" +

    "<cmEDMSCustome rContactPageDet ails>" +

    "<LogEntry> " +

    "<LogEntryHeade r CustomerContact ID=\"8210710493 \">" +

    "</LogEntryHeader> " +

    "<LogEntryR ow rowAction=\"ADD \" CustomerContact ID=\"8210710493 \" LogEntry=\"
    utf-8 characters \">" +

    "</LogEntryRow>" +

    "</LogEntry>" +

    "</cmEDMSCustomerC ontactPageDetai ls>" +

    "</cmEDMSCustomerC ontactPageServi ce>" +

    "</cmEDMSCustomerC ontactPage>" +

    "</SOAP-ENV:Body >" +

    "</SOAP-ENV:Envelope>";

    Console.WriteLi ne(postData);


    byte[] data = encoding.GetByt es(postData);


    // Prepare web request...

    HttpWebRequest myRequest =
    (HttpWebRequest )WebRequest.Cre ate(http://172.18.2.1:7520/server);


    myRequest.Metho d = "POST";

    myRequest.Conte ntType="text/xml";

    myRequest.Conte ntLength = data.Length;

    myRequest.SendC hunked = true;

    myRequest.Trans ferEncoding = "UTF8";


    myRequest.Crede ntials = new NetworkCredenti al("CDX", "pwd");

    Stream newStream=myReq uest.GetRequest Stream();

    // Send the data.

    newStream.Write (data,0,data.Le ngth);

    newStream.Close ();


  • Jon Skeet [C# MVP]

    #2
    Re: send utf-8 string http request

    Mullin Yu <mullin_yu@ctil .com> wrote:[color=blue]
    > I want to submit a utf-8 xml request to a servlet by the following coding.
    > it seesm that the servlet can't recognize it correctly.
    >
    > can i just using string postData = "..... utf-8 data" and then save the
    > files at utf-8 encoded at the vs.net ide?
    >
    > or if i use string, vs.net ide will conside it to be utf-16???
    >
    > UTF8Encoding encoding = new UTF8Encoding();[/color]

    <snip>

    There's no need to create a new UTF8Encoding - just use Encoding.UTF8.

    The rest of your code looks almost correct though. All you need is to
    change your content type so it's "text/xml; encoding=UTF-8" so that the
    servlet can work out what you've sent it in. It should then be fine.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Joerg Jooss

      #3
      Re: send utf-8 string http request

      Jon Skeet [C# MVP] wrote:[color=blue]
      > Mullin Yu <mullin_yu@ctil .com> wrote:[color=green]
      >> I want to submit a utf-8 xml request to a servlet by the following
      >> coding. it seesm that the servlet can't recognize it correctly.
      >>[/color][/color]
      [...][color=blue]
      > There's no need to create a new UTF8Encoding - just use Encoding.UTF8.[/color]

      Actually, there is. Encoding.UTF8 adds a BOM, which you definitely don't
      want when posting data.

      Cheers,

      --
      Joerg Jooss
      joerg.jooss@gmx .net

      Comment

      • Joerg Jooss

        #4
        Re: send utf-8 string http request

        Mullin Yu wrote:[color=blue]
        > I want to submit a utf-8 xml request to a servlet by the following
        > coding. it seesm that the servlet can't recognize it correctly.[/color]

        What's the exact error?

        --
        Joerg Jooss
        joerg.jooss@gmx .net

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: send utf-8 string http request

          Joerg Jooss <joerg.jooss@gm x.net> wrote:[color=blue]
          > Jon Skeet [C# MVP] wrote:[color=green]
          > > Mullin Yu <mullin_yu@ctil .com> wrote:[color=darkred]
          > >> I want to submit a utf-8 xml request to a servlet by the following
          > >> coding. it seesm that the servlet can't recognize it correctly.
          > >>[/color][/color]
          > [...][color=green]
          > > There's no need to create a new UTF8Encoding - just use Encoding.UTF8.[/color]
          >
          > Actually, there is. Encoding.UTF8 adds a BOM, which you definitely don't
          > want when posting data.[/color]

          I don't see any evidence of that when using Encoding.UTF8.G etBytes (as
          the OP used):

          using System;
          using System.Text;

          class Test
          {
          static void Main()
          {
          Console.WriteLi ne (Encoding.UTF8. GetBytes("x").L ength);
          }
          }

          prints 1, rather than 4 which it would print if a BOM were being used.

          It uses a BOM if you create a StreamWriter with it though.

          Even if you were to want to use a version of UTF8Encoding which didn't
          use a BOM, I'd make it a static readonly member of another class rather
          than creating a new instance every time you go through the routine.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Mullin Yu

            #6
            Re: send utf-8 string http request

            the utf8 chinese character became rubblish at oracle db, but it's ok for the
            java program, so that i suspect it's due to my limited knowledge of using
            class and method in .net.

            how about reading from a utf-8 file, i can read it, but it seems having 6
            bytes of byte order marks. am i right?

            thanks!


            "Joerg Jooss" <joerg.jooss@gm x.net> wrote in message
            news:uzbhfAh3DH A.3140@tk2msftn gp13.phx.gbl...[color=blue]
            > Mullin Yu wrote:[color=green]
            > > I want to submit a utf-8 xml request to a servlet by the following
            > > coding. it seesm that the servlet can't recognize it correctly.[/color]
            >
            > What's the exact error?
            >
            > --
            > Joerg Jooss
            > joerg.jooss@gmx .net
            >[/color]


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: send utf-8 string http request

              Mullin Yu <mullin_yu@ctil .com> wrote:[color=blue]
              > the utf8 chinese character became rubblish at oracle db, but it's ok for the
              > java program, so that i suspect it's due to my limited knowledge of using
              > class and method in .net.[/color]

              You need to find out exactly where the characters became "rubbish". The
              way to do that is to output them (as unicode numbers rather than
              glyphs) at various different points, and check where they go wrong.
              [color=blue]
              > how about reading from a utf-8 file, i can read it, but it seems having 6
              > bytes of byte order marks. am i right?[/color]

              No, I believe there should only be 3 bytes of BOM: 0xef 0xbb 0xbf.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Joerg Jooss

                #8
                Re: send utf-8 string http request

                Jon Skeet [C# MVP] wrote:[color=blue]
                > Joerg Jooss <joerg.jooss@gm x.net> wrote:[color=green]
                >> Jon Skeet [C# MVP] wrote:[color=darkred]
                >>> Mullin Yu <mullin_yu@ctil .com> wrote:
                >>>> I want to submit a utf-8 xml request to a servlet by the following
                >>>> coding. it seesm that the servlet can't recognize it correctly.
                >>>>[/color]
                >> [...][color=darkred]
                >>> There's no need to create a new UTF8Encoding - just use
                >>> Encoding.UTF8.[/color]
                >>
                >> Actually, there is. Encoding.UTF8 adds a BOM, which you definitely
                >> don't want when posting data.[/color]
                >
                > I don't see any evidence of that when using Encoding.UTF8.G etBytes (as
                > the OP used):
                >
                > using System;
                > using System.Text;
                >
                > class Test
                > {
                > static void Main()
                > {
                > Console.WriteLi ne (Encoding.UTF8. GetBytes("x").L ength);
                > }
                > }
                >
                > prints 1, rather than 4 which it would print if a BOM were being used.
                >
                > It uses a BOM if you create a StreamWriter with it though.[/color]

                Yeah, got that mixed up.
                [color=blue]
                > Even if you were to want to use a version of UTF8Encoding which didn't
                > use a BOM, I'd make it a static readonly member of another class
                > rather than creating a new instance every time you go through the
                > routine.[/color]

                Sure, but tell it to the original author ;->


                --
                Joerg Jooss
                joerg.jooss@gmx .net

                Comment

                Working...