Requesting a XML document programatically

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • K Viltersten

    #1

    Requesting a XML document programatically

    I have a site that i can request directly
    from my browser's URL field by typing:



    and i wonder how i could obtain the very
    same text that is rendered into the area
    of my browser but assign it to a string
    variable.

    The pseudocode for it as follows.
    string url = http://localhost/Search.aspx;
    Connection c = new Connection (url);
    c.Request();
    string xml = c.GetXmlContent sString();

    I've been googling but haven't had luck
    so i guess it's something awfully basic
    and simple.

    Konrad Viltersten


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Requesting a XML document programatically

    K Viltersten,

    Have you looked at the WebClient class in the System.Net namespace? It
    will do exactly what you want. If you need more control over the request
    you are making, you can use the HttpWebRequest/HttpWebResponse classes in
    the same namespace.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "K Viltersten" <tmp1@vilterste n.comwrote in message
    news:6dmk59F32v anU1@mid.indivi dual.net...
    >I have a site that i can request directly
    from my browser's URL field by typing:
    >

    >
    and i wonder how i could obtain the very
    same text that is rendered into the area
    of my browser but assign it to a string
    variable.
    >
    The pseudocode for it as follows.
    string url = http://localhost/Search.aspx;
    Connection c = new Connection (url);
    c.Request();
    string xml = c.GetXmlContent sString();
    >
    I've been googling but haven't had luck
    so i guess it's something awfully basic
    and simple.
    >
    Konrad Viltersten
    >
    >

    Comment

    • K Viltersten

      #3
      Re: Requesting a XML document programatically

      Have you looked at the WebClient class in the System.Net namespace? It
      will do exactly what you want. If you need more control over the request
      you are making, you can use the HttpWebRequest/HttpWebResponse classes in
      the same namespace.
      I've been trying out some things with the
      namespace you mentioned. Perhaps i'm not far
      off the right code but this far, it's not
      reading the stream as i want it to. This is the
      code that i run.

      string url = "http://localhost/Search.aspx?qwe rt";

      WebRequest request = WebRequest.Crea te(url);

      request.Method = "POST";

      request.Content Type = "text/xml";

      StreamReader reader = new StreamReader(re quest.GetReques tStream());

      string contents = reader.ReadToEn d();

      reader.Close();



      I get errors when creating the stream. Please note,

      not when i READ the stream but when i CREATE the reader...

      Please advise.



      Konrad Viltersten


      Comment

      • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

        #4
        Re: Requesting a XML document programatically

        The GetRequestStrea m function is the one that causes the request to be sent.
        Try changing the Method to GET instead of POST.
        If you still get errors, post the error message. They could be due to
        firewalls/proxys needing authentication or a header missing from the request
        so the message is key to helping with them. They the Method change first
        though and let us know.


        --
        Ciaran O''Donnell



        "K Viltersten" wrote:
        Have you looked at the WebClient class in the System.Net namespace? It
        will do exactly what you want. If you need more control over the request
        you are making, you can use the HttpWebRequest/HttpWebResponse classes in
        the same namespace.
        >
        I've been trying out some things with the
        namespace you mentioned. Perhaps i'm not far
        off the right code but this far, it's not
        reading the stream as i want it to. This is the
        code that i run.
        >
        string url = "http://localhost/Search.aspx?qwe rt";
        >
        WebRequest request = WebRequest.Crea te(url);
        >
        request.Method = "POST";
        >
        request.Content Type = "text/xml";
        >
        StreamReader reader = new StreamReader(re quest.GetReques tStream());
        >
        string contents = reader.ReadToEn d();
        >
        reader.Close();
        >
        >
        >
        I get errors when creating the stream. Please note,
        >
        not when i READ the stream but when i CREATE the reader...
        >
        Please advise.
        >
        >
        >
        Konrad Viltersten
        >
        >
        >

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Requesting a XML document programatically

          On Jul 10, 3:53 pm, "K Viltersten" <t...@vilterste n.comwrote:
          Have you looked at the WebClient class in the System.Net namespace? It
          will do exactly what you want. If you need more control over the request
          you are making, you can use the HttpWebRequest/HttpWebResponse classes in
          the same namespace.
          >
          I've been trying out some things with the
          namespace you mentioned. Perhaps i'm not far
          off the right code but this far, it's not
          reading the stream as i want it to. This is the
          code that i run.
          The Stream you're fetching is the *request* stream - i.e. a stream for
          the data which needs to get *sent* to the web server. That's not
          readable - you're meant to write into it (if you want to send up any
          data).

          You want the *response* stream to read from.

          Jon

          Comment

          • Juan T. Llibre

            #6
            Re: Requesting a XML document programatically

            See this example for the proper syntax :
            http://msdn.microsoft.com/en-us/libr...ebrequest.aspx



            Juan T. Llibre, asp.net MVP
            asp.net faq : http://asp.net.do/faq/
            foros de asp.net, en español : http://asp.net.do/foros/
            =============== =============== ========
            "K Viltersten" <tmp1@vilterste n.comwrote in message news:6dmls1F3bs esU1@mid.indivi dual.net...
            >Have you looked at the WebClient class in the System.Net namespace? It will do exactly what you want. If you need
            >more control over the request you are making, you can use the HttpWebRequest/HttpWebResponse classes in the same
            >namespace.
            >
            I've been trying out some things with the
            namespace you mentioned. Perhaps i'm not far
            off the right code but this far, it's not
            reading the stream as i want it to. This is the
            code that i run.
            >
            string url = "http://localhost/Search.aspx?qwe rt";
            >
            WebRequest request = WebRequest.Crea te(url);
            >
            request.Method = "POST";
            >
            request.Content Type = "text/xml";
            >
            StreamReader reader = new StreamReader(re quest.GetReques tStream());
            >
            string contents = reader.ReadToEn d();
            >
            reader.Close();
            >
            >
            >
            I get errors when creating the stream. Please note,
            >
            not when i READ the stream but when i CREATE the reader...
            >
            Please advise.
            >
            >
            >
            Konrad Viltersten
            >
            >

            Comment

            • Juan T. Llibre

              #7
              Re: Requesting a XML document programatically

              re:
              !The Stream you're fetching is the *request* stream - i.e. a stream for
              !the data which needs to get *sent* to the web server. That's not
              !readable - you're meant to write into it (if you want to send up any data).

              Exactly.

              The link to the MSDN docs I just posted has correct sample code.



              Juan T. Llibre, asp.net MVP
              asp.net faq : http://asp.net.do/faq/
              foros de asp.net, en español : http://asp.net.do/foros/
              =============== =============== ========
              "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
              news:80847bcd-d38b-4cfe-822f-6ea233af82ef@r6 6g2000hsg.googl egroups.com...
              On Jul 10, 3:53 pm, "K Viltersten" <t...@vilterste n.comwrote:
              Have you looked at the WebClient class in the System.Net namespace? It
              will do exactly what you want. If you need more control over the request
              you are making, you can use the HttpWebRequest/HttpWebResponse classes in
              the same namespace.
              >
              I've been trying out some things with the
              namespace you mentioned. Perhaps i'm not far
              off the right code but this far, it's not
              reading the stream as i want it to. This is the
              code that i run.
              The Stream you're fetching is the *request* stream - i.e. a stream for
              the data which needs to get *sent* to the web server. That's not
              readable - you're meant to write into it (if you want to send up any
              data).

              You want the *response* stream to read from.

              Jon


              Comment

              • K Viltersten

                #8
                Re: Requesting a XML document programatically

                This is almost the exact thing i need. Only
                one problem remains. Just as you, guys,
                suspected, there's an issue with login.
                The instance of WebClient i've created
                isn't logged-in yet and that's why i get
                nada from the server.

                So, the question is now as follows. How can
                i retrieve the current credentials from
                "this" so i can copy them into my instance
                of WebClient)?

                The link that Juan so kindly provided, led
                only to DefaultNetworkC redentials and that
                can't be the currently used ones...

                Regards
                Konrad Viltersten




                "Juan T. Llibre" <nomailreplies@ nowhere.comskre v i meddelandet
                news:eYq49Hr4IH A.2060@TK2MSFTN GP02.phx.gbl...
                See this example for the proper syntax :
                http://msdn.microsoft.com/en-us/libr...ebrequest.aspx
                >
                >
                >
                Juan T. Llibre, asp.net MVP
                asp.net faq : http://asp.net.do/faq/
                foros de asp.net, en español : http://asp.net.do/foros/
                =============== =============== ========
                "K Viltersten" <tmp1@vilterste n.comwrote in message
                news:6dmls1F3bs esU1@mid.indivi dual.net...
                >>Have you looked at the WebClient class in the System.Net namespace? It
                >>will do exactly what you want. If you need more control over the request
                >>you are making, you can use the HttpWebRequest/HttpWebResponse classes
                >>in the same namespace.
                >>
                >I've been trying out some things with the
                >namespace you mentioned. Perhaps i'm not far
                >off the right code but this far, it's not
                >reading the stream as i want it to. This is the
                >code that i run.
                >>
                >string url = "http://localhost/Search.aspx?qwe rt";
                >>
                >WebRequest request = WebRequest.Crea te(url);
                >>
                >request.Meth od = "POST";
                >>
                >request.Conten tType = "text/xml";
                >>
                >StreamReader reader = new StreamReader(re quest.GetReques tStream());
                >>
                >string contents = reader.ReadToEn d();
                >>
                >reader.Close() ;
                >>
                >>
                >>
                >I get errors when creating the stream. Please note,
                >>
                >not when i READ the stream but when i CREATE the reader...
                >>
                >Please advise.
                >>
                >>
                >>
                >Konrad Viltersten
                >>
                >>
                >
                >

                Comment

                • K Viltersten

                  #9
                  Re: Requesting a XML document programatically

                  Small amendment. I know i can set the
                  credentials to the current ones by the
                  UseDefaultCrede ntials of my WebClient
                  object.

                  However, allthough it solves the issue
                  for this time, i feel like it's only a
                  lucky coincidence that i succeeded. I
                  prefer to be in full control.

                  Thank you this far and please advise.

                  Regards
                  Konrad Viltersten


                  This is almost the exact thing i need. Only
                  one problem remains. Just as you, guys,
                  suspected, there's an issue with login.
                  The instance of WebClient i've created
                  isn't logged-in yet and that's why i get
                  nada from the server.
                  >
                  So, the question is now as follows. How can
                  i retrieve the current credentials from
                  "this" so i can copy them into my instance
                  of WebClient)?
                  >
                  The link that Juan so kindly provided, led
                  only to DefaultNetworkC redentials and that
                  can't be the currently used ones...
                  >
                  Regards
                  Konrad Viltersten
                  >
                  >
                  >
                  >
                  "Juan T. Llibre" <nomailreplies@ nowhere.comskre v i meddelandet
                  news:eYq49Hr4IH A.2060@TK2MSFTN GP02.phx.gbl...
                  >See this example for the proper syntax :
                  >http://msdn.microsoft.com/en-us/libr...ebrequest.aspx
                  >>
                  >>
                  >>
                  >Juan T. Llibre, asp.net MVP
                  >asp.net faq : http://asp.net.do/faq/
                  >foros de asp.net, en español : http://asp.net.do/foros/
                  >============== =============== =========
                  >"K Viltersten" <tmp1@vilterste n.comwrote in message
                  >news:6dmls1F3b sesU1@mid.indiv idual.net...
                  >>>Have you looked at the WebClient class in the System.Net namespace? It
                  >>>will do exactly what you want. If you need more control over the
                  >>>request you are making, you can use the HttpWebRequest/HttpWebResponse
                  >>>classes in the same namespace.
                  >>>
                  >>I've been trying out some things with the
                  >>namespace you mentioned. Perhaps i'm not far
                  >>off the right code but this far, it's not
                  >>reading the stream as i want it to. This is the
                  >>code that i run.
                  >>>
                  >>string url = "http://localhost/Search.aspx?qwe rt";
                  >>>
                  >>WebRequest request = WebRequest.Crea te(url);
                  >>>
                  >>request.Metho d = "POST";
                  >>>
                  >>request.Conte ntType = "text/xml";
                  >>>
                  >>StreamReade r reader = new StreamReader(re quest.GetReques tStream());
                  >>>
                  >>string contents = reader.ReadToEn d();
                  >>>
                  >>reader.Close( );
                  >>>
                  >>>
                  >>>
                  >>I get errors when creating the stream. Please note,
                  >>>
                  >>not when i READ the stream but when i CREATE the reader...
                  >>>
                  >>Please advise.
                  >>>
                  >>>
                  >>>
                  >>Konrad Viltersten
                  >>>
                  >>>
                  >>
                  >>
                  >
                  >

                  Comment

                  Working...