Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. re: socket connection to a url

re: socket connection to a url

Scheduled Pinned Locked Moved C#
questioncomxmltutorial
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mikemilano
    wrote on last edited by
    #1

    i have been trying to figure out a way to send this xml data to ups for rate quotes. the goal here is to connect to this url, and send data. then receive the data it sends back. i get an exception because i'm trying to connect to a url ( and subdirectories ) instead of an IP. how do i connect to this url and send the data to the appropriate place? here is an example of what i'm attempting:TcpClient tc=new TcpClient("https://www.ups.com/ups.app/xml/Rate"); NetworkStream ns=tc.GetStream(); StreamWriter sw=new StreamWriter(ns); sw.WriteLine(xml); sw.Flush(); StreamReader sr=new StreamReader(ns); txtResponse.Text = sr.ReadLine();

    D B 2 Replies Last reply
    0
    • M mikemilano

      i have been trying to figure out a way to send this xml data to ups for rate quotes. the goal here is to connect to this url, and send data. then receive the data it sends back. i get an exception because i'm trying to connect to a url ( and subdirectories ) instead of an IP. how do i connect to this url and send the data to the appropriate place? here is an example of what i'm attempting:TcpClient tc=new TcpClient("https://www.ups.com/ups.app/xml/Rate"); NetworkStream ns=tc.GetStream(); StreamWriter sw=new StreamWriter(ns); sw.WriteLine(xml); sw.Flush(); StreamReader sr=new StreamReader(ns); txtResponse.Text = sr.ReadLine();

      D Offline
      D Offline
      devvvy
      wrote on last edited by
      #2

      I've got a class that does this, it's in hands of editor right now. Don't know when he's going to release it. Anyway, if you want help, you need to first let us know what the exception was. norm

      M 1 Reply Last reply
      0
      • D devvvy

        I've got a class that does this, it's in hands of editor right now. Don't know when he's going to release it. Anyway, if you want help, you need to first let us know what the exception was. norm

        M Offline
        M Offline
        mikemilano
        wrote on last edited by
        #3

        thanks for the reply, .. here is the exception: System.Net.Sockets.SocketException: The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for at System.Net.Dns.GetHostByName(String hostName) at System.Net.Dns.Resolve(String hostName) at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port) at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port) at ups.Form1.btnSend_Click(Object sender, EventArgs e) in c:\visual studio projects\ups\form1.cs:line 202

        D 1 Reply Last reply
        0
        • M mikemilano

          thanks for the reply, .. here is the exception: System.Net.Sockets.SocketException: The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for at System.Net.Dns.GetHostByName(String hostName) at System.Net.Dns.Resolve(String hostName) at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port) at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port) at ups.Form1.btnSend_Click(Object sender, EventArgs e) in c:\visual studio projects\ups\form1.cs:line 202

          D Offline
          D Offline
          devvvy
          wrote on last edited by
          #4

          Did you try: 1. TcpListener must start listening before TcpClient attempts to connect. 2. Try connect to "localhost" or "127.0.0.1" 3. Port number specified - is it available or has it been taken? norm

          M 1 Reply Last reply
          0
          • D devvvy

            Did you try: 1. TcpListener must start listening before TcpClient attempts to connect. 2. Try connect to "localhost" or "127.0.0.1" 3. Port number specified - is it available or has it been taken? norm

            M Offline
            M Offline
            mikemilano
            wrote on last edited by
            #5

            the same script connects to 'localhost' just fine. i think my problem is connecting to https://www.ups.com/ups.app/xml/Rate if i just use "ups.com" , it connects, but then it hangs because ideally i need to send the data to the "ups.com/ups.app/xml/Rate" application so my app can receive data back. once i create the connection, is there a way to navigate to the 'Rate' app before i send the data?

            D 1 Reply Last reply
            0
            • M mikemilano

              the same script connects to 'localhost' just fine. i think my problem is connecting to https://www.ups.com/ups.app/xml/Rate if i just use "ups.com" , it connects, but then it hangs because ideally i need to send the data to the "ups.com/ups.app/xml/Rate" application so my app can receive data back. once i create the connection, is there a way to navigate to the 'Rate' app before i send the data?

              D Offline
              D Offline
              devvvy
              wrote on last edited by
              #6

              I can't answer your question. If you can connect to your localhost and not to a remote address, ten thousand things can go wrong. I don't think the fact that UPS application is expecting some data (perhaps password) has anything to do with whether or not its TcpListener accepts or reject your call - it must accept the incoming connection (TcpClient) before it can accepts anything it expected. Therefore, should there be a problem, it shouldn't be this. If it is indeed expecting some data, say userID/passed, it should first TcpListener.AcceptConnection. Then, the application should read from the network stream - if it can't find the expected data, drop the connection. But in this case, it has already accepted the connection before it drops it - therefore, Connect shouldn't fail. But TcpClient.Send(...) will - you can't send to a closed socket. norm

              M 1 Reply Last reply
              0
              • D devvvy

                I can't answer your question. If you can connect to your localhost and not to a remote address, ten thousand things can go wrong. I don't think the fact that UPS application is expecting some data (perhaps password) has anything to do with whether or not its TcpListener accepts or reject your call - it must accept the incoming connection (TcpClient) before it can accepts anything it expected. Therefore, should there be a problem, it shouldn't be this. If it is indeed expecting some data, say userID/passed, it should first TcpListener.AcceptConnection. Then, the application should read from the network stream - if it can't find the expected data, drop the connection. But in this case, it has already accepted the connection before it drops it - therefore, Connect shouldn't fail. But TcpClient.Send(...) will - you can't send to a closed socket. norm

                M Offline
                M Offline
                mikemilano
                wrote on last edited by
                #7

                thanks, but i think i am miscommunicating here. i can connect to ups.com , .. just not the 'Rate' program the UPS documentation calls for me to request information from. all the authentication is done in the xml string i'm sending to them. the problem seems to be with me trying to connect to a sub directory of a domain. so i am probably supposed to connect to ups.com , .. but i must figure out how to send the data to that Rate app in the sub directories. thanks a lot for your time though!

                D 1 Reply Last reply
                0
                • M mikemilano

                  thanks, but i think i am miscommunicating here. i can connect to ups.com , .. just not the 'Rate' program the UPS documentation calls for me to request information from. all the authentication is done in the xml string i'm sending to them. the problem seems to be with me trying to connect to a sub directory of a domain. so i am probably supposed to connect to ups.com , .. but i must figure out how to send the data to that Rate app in the sub directories. thanks a lot for your time though!

                  D Offline
                  D Offline
                  devvvy
                  wrote on last edited by
                  #8

                  no problem. Sorry I can't be of any help here. Good luck. norm

                  B 1 Reply Last reply
                  0
                  • D devvvy

                    no problem. Sorry I can't be of any help here. Good luck. norm

                    B Offline
                    B Offline
                    Blake Coverett
                    wrote on last edited by
                    #9

                    Ack - norm, did you actually understand his code at all? -- -Blake (com/bcdev/blake)

                    D 1 Reply Last reply
                    0
                    • M mikemilano

                      i have been trying to figure out a way to send this xml data to ups for rate quotes. the goal here is to connect to this url, and send data. then receive the data it sends back. i get an exception because i'm trying to connect to a url ( and subdirectories ) instead of an IP. how do i connect to this url and send the data to the appropriate place? here is an example of what i'm attempting:TcpClient tc=new TcpClient("https://www.ups.com/ups.app/xml/Rate"); NetworkStream ns=tc.GetStream(); StreamWriter sw=new StreamWriter(ns); sw.WriteLine(xml); sw.Flush(); StreamReader sr=new StreamReader(ns); txtResponse.Text = sr.ReadLine();

                      B Offline
                      B Offline
                      Blake Coverett
                      wrote on last edited by
                      #10

                      I'm afraid you are using entirely the wrong tool for the job. URL's and Ports are in no way interchangable. You need to be using WebRequest not TcpClient. TCP is a stream protocol between an IP address/port pair on one computer and a IP address/port pair on another computer. HTTPS is an application level protocol between that runs inside an encrypted tunnel that's inside a TCP connection. Since you are sending XML to this URL I suspect it is a web service of some flavor, and even easier you should be using a proxy generated from their WSDL description of the service. -- -Blake (com/bcdev/blake)

                      1 Reply Last reply
                      0
                      • B Blake Coverett

                        Ack - norm, did you actually understand his code at all? -- -Blake (com/bcdev/blake)

                        D Offline
                        D Offline
                        devvvy
                        wrote on last edited by
                        #11

                        no i don't, do you norm

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups