提问人:Bob 提问时间:10/28/2023 最后编辑:Bob 更新时间:11/7/2023 访问量:72
.NET、Maui、Android:使用 POST 获取 URI 的 Java 异常
.NET, Maui, Android: getting Java exception for URI using POST
问:
问题
我正在使用样板代码在 .NET Maui 应用程序中创建资源 (POST),并且在运行以下代码行时出现异常:
HttpResponseMessage response = await httpClient.SendAsync(message);
例外情况如下:
Java.Lang.IllegalArgumentException:toASCII 输入无效:changed_for_privacy.com ---> Android.Icu.Text.StringPrepParseException:输入不符合 STD 3 ASCII 规则
变量是 POST 的 URI。在本例中,URI 当然具有 URL。我认为问题在于 URL 包含并下划线字符。例如,该消息如下所示:Message
https://my_site.com/proceed/to/endpoint
基于异常、堆栈跟踪(见下文)和一些谷歌搜索(请参阅本问题末尾的链接),我认为 Android 在处理 URL 中的下划线时遇到了问题。还有其他人也有类似的问题,但在过去的几年里我找不到任何东西。有针对该问题的解决方案,但它们要么不是 .NET,要么解决方案是“好吧,您不应该使用带有下划线的 URL”。
需要明确的是,URL 中有一个下划线,而不是 URI。
无论如何,“不使用带有下划线的 URL”不是我的选择。
我的代码
代码相当样板:
namespace my_project.Service;
internal class MyService
{
private readonly HttpClient httpClient;
public MyService()
{
httpClient = new();
}
public async Task GetAuthTokenPOST()
{
AccessTokenBodyPost accessTokenBodyModel = new()
{
grant_type = "password",
username = "username"
};
try
{
HttpRequestMessage message = new(HttpMethod.Post, "https://changed_for_privacy.com/core/connect/token");
message.Content = JsonContent.Create(accessTokenBodyModel);
// EXCEPTION THROWN ON THIS LINE
HttpResponseMessage response = await httpClient.SendAsync(message);
}
catch (Exception ex)
{
await ExitProgram.ErrorExitProgram();
}
}
}
编辑:URL中的下划线是问题所在
我设法让人们启动了一个全新的测试服务器,没有任何下划线......它有效。
我想教训是:避免在 URL 中使用下划线?
以下是堆栈跟踪的更多内容。(我不能发布整个东西 - 当我尝试时,Stack Overflow 一直给我一条错误消息 - 但如果有人想看到它,我会再试一次,即使它基本上只是我在下面重复发布的内容。
Java.Lang.IllegalArgumentException: Invalid input to toASCII: changed_for_privacy.com
---> Android.Icu.Text.StringPrepParseException: The input does not conform to the STD 3 ASCII rules
--- End of managed Android.Icu.Text.StringPrepParseException stack trace ---
The input does not conform to the STD 3 ASCII rules. line: 0. preContext: . postContext: 3_identity
at android.icu.impl.IDNA2003.convertToASCII(IDNA2003.java:218)
at android.icu.impl.IDNA2003.convertIDNToASCII(IDNA2003.java:276)
at android.icu.text.IDNA.convertIDNToASCII(IDNA.java:654)
at com.android.icu.text.ExtendedIDNA.convertIDNToASCII(ExtendedIDNA.java:50)
at java.net.IDN.toASCII(IDN.java:108)
at javax.net.ssl.SNIHostName.<init>(SNIHostName.java:99)
at com.android.org.conscrypt.Platform.getSSLParameters(Platform.java:187)
at com.android.org.conscrypt.ConscryptEngine.getSSLParameters(ConscryptEngine.java:525)
at com.android.org.conscrypt.ConscryptEngineSocket.getSSLParameters(ConscryptEngineSocket.java:186)
at com.android.okhttp.internal.Platform.configureTlsExtensions(Platform.java:121)
at com.android.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:191)
at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:153)
at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:90)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:30)
--- End of managed Android.Icu.Text.StringPrepParseException stack trace ---
The input does not conform to the STD 3 ASCII rules. line: 0. preContext: . postContext: 3_identity
at android.icu.impl.IDNA2003.convertToASCII(IDNA2003.java:218)
at android.icu.impl.IDNA2003.convertIDNToASCII(IDNA2003.java:276)
at android.icu.text.IDNA.convertIDNToASCII(IDNA.java:654)
at com.android.icu.text.ExtendedIDNA.convertIDNToASCII(ExtendedIDNA.java:50)
at java.net.IDN.toASCII(IDN.java:108)
at javax.net.ssl.SNIHostName.<init>(SNIHostName.java:99)
at com.android.org.conscrypt.Platform.getSSLParameters(Platform.java:187)
at com.android.org.conscrypt.ConscryptEngine.getSSLParameters(ConscryptEngine.java:525)
at com.android.org.conscrypt.ConscryptEngineSocket.getSSLParameters(ConscryptEngineSocket.java:186)
at com.android.okhttp.internal.Platform.configureTlsExtensions(Platform.java:121)
at com.android.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:191)
at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:153)
at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:90)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:30)
--- End of inner exception stack trace ---
Java.Lang.IllegalArgumentException: Invalid input to toASCII: changed_for_privacy.com
---> Android.Icu.Text.StringPrepParseException: The input does not conform to the STD 3 ASCII rules
--- End of managed Android.Icu.Text.StringPrepParseException stack trace ---
The input does not conform to the STD 3 ASCII rules. line: 0. preContext: . postContext: 3_identity
at android.icu.impl.IDNA2003.convertToASCII(IDNA2003.java:218)
at android.icu.impl.IDNA2003.convertIDNToASCII(IDNA2003.java:276)
at android.icu.text.IDNA.convertIDNToASCII(IDNA.java:654)
at com.android.icu.text.ExtendedIDNA.convertIDNToASCII(ExtendedIDNA.java:50)
at java.net.IDN.toASCII(IDN.java:108)
at javax.net.ssl.SNIHostName.<init>(SNIHostName.java:99)
at com.android.org.conscrypt.Platform.getSSLParameters(Platform.java:187)
at com.android.org.conscrypt.ConscryptEngine.getSSLParameters(ConscryptEngine.java:525)
at com.android.org.conscrypt.ConscryptEngineSocket.getSSLParameters(ConscryptEngineSocket.java:186)
at com.android.okhttp.internal.Platform.configureTlsExtensions(Platform.java:121)
at com.android.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:191)
at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:153)
at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:90)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:30)
--- End of managed Android.Icu.Text.StringPrepParseException stack trace ---
The input does not conform to the STD 3 ASCII rules. line: 0. preContext: . postContext: 3_identity
at android.icu.impl.IDNA2003.convertToASCII(IDNA2003.java:218)
at android.icu.impl.IDNA2003.convertIDNToASCII(IDNA2003.java:276)
at android.icu.text.IDNA.convertIDNToASCII(IDNA.java:654)
at com.android.icu.text.ExtendedIDNA.convertIDNToASCII(ExtendedIDNA.java:50)
at java.net.IDN.toASCII(IDN.java:108)
at javax.net.ssl.SNIHostName.<init>(SNIHostName.java:99)
at com.android.org.conscrypt.Platform.getSSLParameters(Platform.java:187)
at com.android.org.conscrypt.ConscryptEngine.getSSLParameters(ConscryptEngine.java:525)
at com.android.org.conscrypt.ConscryptEngineSocket.getSSLParameters(ConscryptEngineSocket.java:186)
at com.android.okhttp.internal.Platform.configureTlsExtensions(Platform.java:121)
at com.android.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:191)
at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:153)
at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:90)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:30)
--- End of inner exception stack trace ---
有谁知道这是什么和/或如何解决它?我已经尝试了涉及 httpClient 和“编码”的所有方法,或者使用 ASCII 字符而不是下划线。
在这里,我得到了一个问题,即问题出在URL中的下划线上。似乎人们曾经有过这个问题,但最近没有。
在这里,我得到的印象是这不是一个错误或错误,这只是世界的运作方式:如果你想使用Android,域中没有下划线。我想知道这是为什么?这让我最后两个月的工作变得毫无用处。按照字面。
请注意,该 URL 是可 ping 的,并且 POST 适用于 Postman。
答: 暂无答案
评论