无法使用VPN代理连接并从远程本地REST API获取数据
我正在尝试连接到远程本地服务器,以从其REST API中获取数据。我正在进行ASP.NET MVC项目,并使用RestSharp。当我建立到远程本地服务器的VPN连接(通过默认的Windows VPN连接)时,我可以使用Postman并成功从其API中获取数据。我将使用此数据将新记录添加到我的数据库中,或者仅向我的项目中的用户显示。
但是如果没有VPN连接,我将无法使用RestSharp连接到远程本地服务器。它给了我超时错误。我的restSharp配置的C#代码类似:
public async Task<ActionResult> PersonalList()
{
var options = new RestClientOptions("http://5.2.xxx.xx") //Remote local server IP
{
Proxy = new WebProxy("192.168.1.240", 12001), //Remote local API url and port
ThrowOnAnyError = true
};
var client = new RestClient(options);
client.Authenticator = new HttpBasicAuthenticator("myUserNameWhichIuseAtWindowsVPNConnection", "myPasswordWhichIuseAtWindowsVPNConnection");
var req = new RestRequest("/datasnap/rest/ServerMethods/GetPersonalList", Method.Get); //Get personal list API url
var resp = await client.ExecuteAsync(req);
return View();
}
我没有更改或添加任何配置到function或web.config默认dracorentals。
现在,我对“我正在尝试正确的方法?或者这种与远程本地服务器的连接是不可能的?”。如果这是错误的方式,我应该如何部署这个项目?如果我将其部署到他们的本地服务器,他们只在本地使用它(在Additon中,当我通过VPN连接到本地服务器时我无法使用他们的Internet连接。我只能使用他们的“ 192.168.1.240:12001”。)。)。)。)。)。 。我希望他们不仅在本地使用公司时使用它。
也许由于我的困惑,我混合了很多不同的问题,为此感到抱歉。但是,如果您可以建议我解决这种情况的最佳实践,我会很高兴。
I'm trying to connect to a remote local server to get data from their Rest API. I'm on a ASP.NET MVC project and using RestSharp. When I establish VPN connection to the remote local server (via default Windows VPN connection), I'm be able to use Postman and successfully get data from their APIs. I will use this data to add new records to my DB or only display to users in my project.
But without VPN connection, I can't connect to the remote local server with Restsharp. It gives me timeout error. My C# codes for RestSharp configurations is like:
public async Task<ActionResult> PersonalList()
{
var options = new RestClientOptions("http://5.2.xxx.xx") //Remote local server IP
{
Proxy = new WebProxy("192.168.1.240", 12001), //Remote local API url and port
ThrowOnAnyError = true
};
var client = new RestClient(options);
client.Authenticator = new HttpBasicAuthenticator("myUserNameWhichIuseAtWindowsVPNConnection", "myPasswordWhichIuseAtWindowsVPNConnection");
var req = new RestRequest("/datasnap/rest/ServerMethods/GetPersonalList", Method.Get); //Get personal list API url
var resp = await client.ExecuteAsync(req);
return View();
}
I haven't changed or add any configuration to function or web.config for default credientals.
Now, I'm confused about "Am I trying the right way? Or is this type of connection to remote local server is impossible?". If this is wrong way, how should I deploy this project? If I deploy it to their local server, they only use it locally (In additon, I can't use their internet connection when I connect to their local server via VPN. I can use only their "192.168.1.240:12001".). I want them to use it when they are out of their company, not only locally.
Maybe I mixed a lot of different problems because of my confussion, sorry for that. But I will be very glad if you could suggest me what is the best practice for the solution of this situation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里不是很苛刻,而是快速术语校正...当您编写:“远程本地服务器”时,我想您要说的是您想从“本地计算机”上连接到远程服务器'远程服务器' VPN。当您的VPN客户端处于活动状态时,您只能做到这一点。将两个单词放在一起,“远程本地”至少对我来说是令人困惑的。 :)
当VPN客户端打开时 - 它将修改您的本地操作系统并提供一种机制(确切的机制取决于您的VPN),以使您的操作系统了解IP“ 5.2.xxx.xx”是可以从您的本地寻址的电脑。您的操作系统将拾取您告诉其发送到“ 5.2.xxx.xx”的数据包,并将其运送到VPN的另一侧。一旦在另一侧,VPN就会知道如何将您的流量发送到实际目标。
当您关闭VPN时,您会失去VPN为操作系统提供的“魔术”。突然,您的操作系统不了解如何将IP数据包从本地计算机发送到“ 5.2.xxx.xx”。这将导致您的代码返回某种“我无法连接”(例如超时,或“无路由到主机”或类似的错误)
很难回答这个问题。您可以将服务器暴露于开放的Internet上,但是我怀疑这是您不想做的,因为您使用VPN,所以不想做?
如果您连接到VPN时无法使用“他们的”连接(我认为您的意思是您的Internet连接?)听起来VPN不是“拆分隧道”。这意味着您的所有流量都通过VPN进行,并且您的流量可能被VPN的安全规则拦截了(并阻止您)。
他们将需要使用VPN,否则他们将无法访问。那就是它的工作方式。
这是您要问的“非常大”的话题。您可能值得您观看有关VPN的工作方式和IP网络的一些YouTube视频。我认为这会帮助您理解。 :|我知道这不是一个很好的答案,但这可能是您应该做的。
Not to be harsh here but a quick terminology correction... When you write: "remote local server" I think what you're saying is that you want to connect to a remote server 'remote server' from your 'local computer' over a VPN. You will only be able to do that when your VPN client is active. Putting both words together like that, "remote local", is to me at least, confusing. :)
When the VPN client is on - it will modify your local operating system and provide a mechanism (exact mechanism depends on your VPN) for your operating system to understand that the IP "5.2.xxx.xx" is somehow addressable from your local computer. Your operating system will pick up packets that you tell it to send to "5.2.xxx.xx" and it will transport them to the other side of your VPN. Once at the other side, the VPN knows how to send your traffic to the actual target.
When you turn your VPN off - you lose that "magic" the VPN is providing for your operating system. Suddenly your OS doesn't understand how to send IP packets from your local computer to "5.2.xxx.xx". That will result in your code returning some kind of "I can't connect" (like a timeout, or 'no route to host' or some error like that)
Hard to answer this question. You could expose the server to the open internet, but I suspect that's specifically what you don't want to do since you're using a VPN?
If you can't use "their" internet connection when connected to the VPN (I would think you mean YOUR internet connection?) it sounds like the VPN is not a "split-tunnel". That means ALL your traffic is going through their VPN and your traffic is probably getting intercepted by the VPN's security rules (and blocking you).
They would need to be on the VPN or else they won't have access. That's how it works.
This is a 'pretty big' topic you're asking about. It probably would be worthwhile for you to watch some youtube videos about how VPNs work and IP networking in general. I think that'd help you understand. :| I know that's not a great answer but it's what you probably should do.
使用rasdial的一种方法(您也可以使用netsh命令)
不要忘记使用try-catch块来启用您的错误。
A way to go about using rasdial (you can use netsh command as well)
Don't forget to use a try-catch block to enable you catch errors as would want.