本文介绍了从URL获取端点IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用TCP客户端进行http发布。我得到的结果与例外情况不同。
没有HTTP 200 OK ...

Using a TCP Client for http post. I get a different result than excepted.No HTTP 200 OK...

这是我的请求:

GET / HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: de-DE
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
Host:www.mywebsite.com
Connection: Keep-Alive
Cache-Control: max-age=0

以下是我的回复:

HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Location: http://somewhere.com (url changed)
Content-Length: 0
Date: Fri, 03 Aug 2012 10:38:14 GMT

它告诉我重定向到位置。
下一个请求应该是并参考www.mywebsite的GET。 com?

Its tell me to redirect to "Location".The next request should be a GET to http://somewhere.com with a refer to www.mywebsite.com ?

另一种情况是https链接。它通常与HostEntry返回的IP完全不同。如何获得IP?在c#中是否有一种简单的方法?

A other scenario is a https link. Its often a total different IP than the HostEntry returns. How to get the IP ? Is there an easy way in c# ?

推荐答案

是的302意味着网址不是在你想要的位置(主机可能已移动它,或者更喜欢使用www而不是顶点)。因此,只需对新URL使用HTTP GET即可。

Yes a 302 means that the url is not at the location you want (the host may have moved it, or prefers you use www instead of the apex). So just use a HTTP GET for the new URL.

就IP地址而言,我担心它不是那么简单,你可以拥有多个主机名的IP地址

In terms of the IP Address, I'm afraid its not so simple, you can have multiple IP Addresses for a hostname

string stackoverflow= "stackoverflow.com"
IPAddress[] addresslist = Dns.GetHostAddresses(stackoverflow);

foreach (IPAddress theaddress in addresslist)
{
   Console.WriteLine(theaddress.ToString());
}

在很多情况下,使用多个IP(主要是负载平衡)的情况下)。

There are lots of situations where more than one IP is used (mainly load balancing situations).

这篇关于从URL获取端点IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:05