re: socket connection to a url
-
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();
-
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();
-
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
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
-
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
-
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
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?
-
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?
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
-
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
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!
-
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!
-
Ack - norm, did you actually understand his code at all? -- -Blake (com/bcdev/blake)
-
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();
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)
-
Ack - norm, did you actually understand his code at all? -- -Blake (com/bcdev/blake)