1and1 使用 ASP.NET CORE 将请求限制到另一台服务器
1and1 restricts request to another server using ASP.NET CORE
我正在尝试通过我的 .NET Core 应用程序中的 SDK 向 Paypal 端点发出请求。该请求在本地服务器上工作得很好。但是当我在 1and1 托管服务器上发布应用程序时,出现此错误。
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
我发现的原因是1and1不允许直接调用所以我需要使用代理。
有些人已经问过同样的问题。但是我没有真正的答案。
这里有两个:
- A connection attempt failed because the connected party did not properly respond
- https://forums.asp.net/t/2138734.aspx?A+connection+attempt+failed+because+the+connected+party+did+not+properly+respond+after+a+period+of+time
我也联系了 1and1 寻求支持(2 次)。他们向我发送了我已经找到并尝试过的相同 link(德语)。没有成功。
这里有一些我试过的senairos:
1.将代理服务器添加到 web.config
文件
我的配置文件看起来像这样
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath=".\MyApp.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
<system.net>
<defaultProxy>
<proxy proxyaddress="http://1and1proxy:123"
bypassonlocal="true"/>
</defaultProxy>
</system.net>
</configuration>
2。将 AspNetCore.Proxy
添加到 startup.cs
文件
public void ConfigureServices(IServiceCollection services) {
...
services.AddProxies();
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
...
app.RunProxy(proxy => proxy.UseHttp("http://1and1proxy:123"));
...
}
3。向另一台服务器发出另一个简单请求(使用 webproxy
但也失败了)
public async Task < IActionResult > callingGet() {
var proxiedHttpClientHandler = new HttpClientHandler() {
UseProxy = true
};
proxiedHttpClientHandler.Proxy = new WebProxy("http://1and1proxy:123");
HttpClient client = new HttpClient(proxiedHttpClientHandler);
using
var client = new HttpClient();
var result = await client.GetAsync("https://reqres.in/api/users/2");
return View(new Model {
Message = result.ToString()
});
}
我是不是漏掉了什么,还是必须换到其他供应商?
PS: 这是我第一次发布网站
事实证明第三个有效(但我仍然不知道为什么第一个和第二个没有)。
所以我在 GitHub page 上创建了一个问题,询问我是否可以选择在调用 PayPal 服务请求时创建代理。我没有收到任何 SDK 开发人员的回复,只有一位成员有同样的请求。他不得不自己创建这个选项,因为还没有使用 PayPal .Net SDK 设置代理的官方方法。所以我要在这里重新发布他的答案。
我认为它可以帮助某人。再次感谢WSSC-Jonathan
。
using System;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using PayPalCheckoutSdk.Core;
namespace MongMi.Application.Paypal
{
public class ProxiedPayPalHttpClient : PayPalHttp.HttpClient
{
private WebProxy _proxy;
private PayPalEnvironment _environment;
private PayPalHttp.IInjector _gzipInjector;
private PayPalHttp.IInjector _authInjector;
public ProxiedPayPalHttpClient(PayPalEnvironment environment, WebProxy proxy)
: base(environment)
{
_environment = environment;
_proxy = proxy;
OverrideClient();
SetupInjectors();
}
public ProxiedPayPalHttpClient(PayPalEnvironment environment, string url, string username = "", string password = "")
: base(environment)
{
_environment = environment;
_proxy = SetupProxy(url, username, password);
OverrideClient();
SetupInjectors();
}
public WebProxy SetupProxy(string url, string username, string password)
{
var proxy = new WebProxy(url)
{
BypassProxyOnLocal = true,
UseDefaultCredentials = true
};
if (!string.IsNullOrEmpty(username) || !string.IsNullOrEmpty(password))
proxy.Credentials = new NetworkCredential(username, password);
return proxy;
}
private void OverrideClient()
{
var handler = new HttpClientHandler
{
Proxy = _proxy
};
var client = new HttpClient(handler, true) { BaseAddress = new Uri(environment.BaseUrl()) };
client.DefaultRequestHeaders.Add("User-Agent", GetUserAgent());
SetClient(client);
}
private void SetupInjectors()
{
_gzipInjector = new GzipInjector();
_authInjector = new AuthorizationInjector(_proxy, _environment);
AddInjector(_gzipInjector);
AddInjector(_authInjector);
}
protected void SetClient(HttpClient client)
{
var type = GetType();
while (type != null)
{
var field = type.GetField("client", BindingFlags.Instance | BindingFlags.NonPublic);
if (field != null)
{
field.SetValue(this, client);
break;
}
type = type.BaseType;
}
}
private class AuthorizationInjector : PayPalHttp.IInjector
{
private WebProxy _proxy;
private PayPalEnvironment _environment;
private AccessToken _accessToken;
public AuthorizationInjector(WebProxy proxy, PayPalEnvironment environment)
{
_environment = environment;
_proxy = proxy;
}
public void Inject(PayPalHttp.HttpRequest request)
{
if (!request.Headers.Contains("Authorization") && !(request is AccessTokenRequest || request is RefreshTokenRequest))
{
if (_accessToken == null || _accessToken.IsExpired())
_accessToken = FetchAccessToken().Result<AccessToken>();
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken.Token);
}
}
private PayPalHttp.HttpResponse FetchAccessToken()
{
var client = new ProxiedPayPalHttpClient(_environment, _proxy);
var request = new AccessTokenRequest(_environment, null);
return Task.Run(async () => await client.Execute(request).ConfigureAwait(false)).Result;
}
}
private class GzipInjector : PayPalHttp.IInjector
{
public void Inject(PayPalHttp.HttpRequest request)
{
request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
}
}
}
}
我正在尝试通过我的 .NET Core 应用程序中的 SDK 向 Paypal 端点发出请求。该请求在本地服务器上工作得很好。但是当我在 1and1 托管服务器上发布应用程序时,出现此错误。
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
我发现的原因是1and1不允许直接调用所以我需要使用代理。 有些人已经问过同样的问题。但是我没有真正的答案。
这里有两个:
- A connection attempt failed because the connected party did not properly respond
- https://forums.asp.net/t/2138734.aspx?A+connection+attempt+failed+because+the+connected+party+did+not+properly+respond+after+a+period+of+time
我也联系了 1and1 寻求支持(2 次)。他们向我发送了我已经找到并尝试过的相同 link(德语)。没有成功。
这里有一些我试过的senairos:
1.将代理服务器添加到 web.config
文件
我的配置文件看起来像这样
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath=".\MyApp.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
<system.net>
<defaultProxy>
<proxy proxyaddress="http://1and1proxy:123"
bypassonlocal="true"/>
</defaultProxy>
</system.net>
</configuration>
2。将 AspNetCore.Proxy
添加到 startup.cs
文件
public void ConfigureServices(IServiceCollection services) {
...
services.AddProxies();
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
...
app.RunProxy(proxy => proxy.UseHttp("http://1and1proxy:123"));
...
}
3。向另一台服务器发出另一个简单请求(使用 webproxy
但也失败了)
public async Task < IActionResult > callingGet() {
var proxiedHttpClientHandler = new HttpClientHandler() {
UseProxy = true
};
proxiedHttpClientHandler.Proxy = new WebProxy("http://1and1proxy:123");
HttpClient client = new HttpClient(proxiedHttpClientHandler);
using
var client = new HttpClient();
var result = await client.GetAsync("https://reqres.in/api/users/2");
return View(new Model {
Message = result.ToString()
});
}
我是不是漏掉了什么,还是必须换到其他供应商? PS: 这是我第一次发布网站
事实证明第三个有效(但我仍然不知道为什么第一个和第二个没有)。
所以我在 GitHub page 上创建了一个问题,询问我是否可以选择在调用 PayPal 服务请求时创建代理。我没有收到任何 SDK 开发人员的回复,只有一位成员有同样的请求。他不得不自己创建这个选项,因为还没有使用 PayPal .Net SDK 设置代理的官方方法。所以我要在这里重新发布他的答案。
我认为它可以帮助某人。再次感谢WSSC-Jonathan
。
using System;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using PayPalCheckoutSdk.Core;
namespace MongMi.Application.Paypal
{
public class ProxiedPayPalHttpClient : PayPalHttp.HttpClient
{
private WebProxy _proxy;
private PayPalEnvironment _environment;
private PayPalHttp.IInjector _gzipInjector;
private PayPalHttp.IInjector _authInjector;
public ProxiedPayPalHttpClient(PayPalEnvironment environment, WebProxy proxy)
: base(environment)
{
_environment = environment;
_proxy = proxy;
OverrideClient();
SetupInjectors();
}
public ProxiedPayPalHttpClient(PayPalEnvironment environment, string url, string username = "", string password = "")
: base(environment)
{
_environment = environment;
_proxy = SetupProxy(url, username, password);
OverrideClient();
SetupInjectors();
}
public WebProxy SetupProxy(string url, string username, string password)
{
var proxy = new WebProxy(url)
{
BypassProxyOnLocal = true,
UseDefaultCredentials = true
};
if (!string.IsNullOrEmpty(username) || !string.IsNullOrEmpty(password))
proxy.Credentials = new NetworkCredential(username, password);
return proxy;
}
private void OverrideClient()
{
var handler = new HttpClientHandler
{
Proxy = _proxy
};
var client = new HttpClient(handler, true) { BaseAddress = new Uri(environment.BaseUrl()) };
client.DefaultRequestHeaders.Add("User-Agent", GetUserAgent());
SetClient(client);
}
private void SetupInjectors()
{
_gzipInjector = new GzipInjector();
_authInjector = new AuthorizationInjector(_proxy, _environment);
AddInjector(_gzipInjector);
AddInjector(_authInjector);
}
protected void SetClient(HttpClient client)
{
var type = GetType();
while (type != null)
{
var field = type.GetField("client", BindingFlags.Instance | BindingFlags.NonPublic);
if (field != null)
{
field.SetValue(this, client);
break;
}
type = type.BaseType;
}
}
private class AuthorizationInjector : PayPalHttp.IInjector
{
private WebProxy _proxy;
private PayPalEnvironment _environment;
private AccessToken _accessToken;
public AuthorizationInjector(WebProxy proxy, PayPalEnvironment environment)
{
_environment = environment;
_proxy = proxy;
}
public void Inject(PayPalHttp.HttpRequest request)
{
if (!request.Headers.Contains("Authorization") && !(request is AccessTokenRequest || request is RefreshTokenRequest))
{
if (_accessToken == null || _accessToken.IsExpired())
_accessToken = FetchAccessToken().Result<AccessToken>();
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken.Token);
}
}
private PayPalHttp.HttpResponse FetchAccessToken()
{
var client = new ProxiedPayPalHttpClient(_environment, _proxy);
var request = new AccessTokenRequest(_environment, null);
return Task.Run(async () => await client.Execute(request).ConfigureAwait(false)).Result;
}
}
private class GzipInjector : PayPalHttp.IInjector
{
public void Inject(PayPalHttp.HttpRequest request)
{
request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
}
}
}
}