使用wirseshark对token获取过程进行抓包,这个连接到底怎么了?

qq_22216299 2017-02-22 02:46:49
在向服务区获取token时一直不成功,用Wirseshark抓取数据包,却发现,建立连接后,客户端向服务器发送数据,按道理服务器应返回数据,可却返回了ACK,然后连接就关闭了。导致程序一直挂着进行不下去。
抓取的数据包如下图:


部分程序代码如下:
        public async Task<string> GetAccessTokenAsync()
{
if (SubscriptionKey == string.Empty) return string.Empty;

// Re-use the cached token if there is one.
if ((DateTime.Now - storedTokenTime) < TokenCacheDuration)
{
return storedTokenValue;
}

using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{

request.Method = HttpMethod.Post;
request.RequestUri = ServiceUrl;
request.Content = new StringContent(string.Empty);
request.Headers.TryAddWithoutValidation(OcpApimSubscriptionKeyHeader, this.SubscriptionKey);
client.Timeout = TimeSpan.FromSeconds(2);

var response = await client.SendAsync(request);

this.RequestStatusCode = response.StatusCode;
response.EnsureSuccessStatusCode();
var token = await response.Content.ReadAsStringAsync();
storedTokenTime = DateTime.Now;
storedTokenValue = "Bearer " + token;
return storedTokenValue;
}
}


最后放一张其他程序成功获取token的wirseshark抓包图:

盼请各位大牛分析分析。
...全文
614 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_22216299 2017-02-23
  • 打赏
  • 举报
回复
引用 12 楼 xdashewan 的回复:
程序没有运行到底?response有没dispose掉?

没有dispose啊,调试输出为线程退出,返回值为-1
xdashewan 2017-02-23
  • 打赏
  • 举报
回复
程序没有运行到底?response有没dispose掉?
qq_22216299 2017-02-23
  • 打赏
  • 举报
回复
引用 10 楼 xdashewan 的回复:
[quote=引用 9 楼 qq_22216299 的回复:]
我重新设置了timeout为5s,这次抓包抓到有返回数据了,但是,不知道为什么程序还是在等待

你断点调试下等待在哪个方法里,看看收到数据有没回复ACK或者FIN什么的[/quote]

回复了ACK,没有关闭连接。
xdashewan 2017-02-23
  • 打赏
  • 举报
回复
引用 9 楼 qq_22216299 的回复:
我重新设置了timeout为5s,这次抓包抓到有返回数据了,但是,不知道为什么程序还是在等待
你断点调试下等待在哪个方法里,看看收到数据有没回复ACK或者FIN什么的
qq_22216299 2017-02-23
  • 打赏
  • 举报
回复
引用 7 楼 xdashewan 的回复:
你client.Timeout设了多少,不会是因为timeout了自动关闭吧
我重新设置了timeout为5s,这次抓包抓到有返回数据了,但是,不知道为什么程序还是在等待
xdashewan 2017-02-23
  • 打赏
  • 举报
回复
引用 13 楼 qq_22216299 的回复:
没有dispose啊,调试输出为线程退出,返回值为-1
哪个方法返回-1,你去msdn查下返回值的意义
qq_22216299 2017-02-22
  • 打赏
  • 举报
回复
引用 7 楼 xdashewan 的回复:
你client.Timeout设了多少,不会是因为timeout了自动关闭吧
    public class AzureAuthToken
    {
        /// URL of the token service {Method: POST, RequestUri: 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken', Version: 1.1, Content: System.Net.Http.StringContent, Headers:{  Ocp-Apim-Subscription-Key: baac680188ff492ab5f6e50a5609da3d  Content-Type: text/plain; charset=utf-8}}
        private static readonly Uri ServiceUrl = new Uri("https://api.cognitive.microsoft.com/sts/v1.0/issueToken");
        /// Name of header used to pass the subscription key to the token service
        private const string OcpApimSubscriptionKeyHeader = "Ocp-Apim-Subscription-Key";
        /// After obtaining a valid token, this class will cache it for this duration.
        /// Use a duration of 5 minutes, which is less than the actual token lifetime of 10 minutes.
        private static readonly TimeSpan TokenCacheDuration = new TimeSpan(0, 5, 0);

        /// Cache the value of the last valid token obtained from the token service.
        private string storedTokenValue = string.Empty;
        /// When the last valid token was obtained.
        private DateTime storedTokenTime = DateTime.MinValue;

        /// Gets the subscription key.
        public string SubscriptionKey { get; private set; } = string.Empty;

        /// Gets the HTTP status code for the most recent request to the token service.
        public HttpStatusCode RequestStatusCode { get; private set; }

        /// <summary>
        /// Creates a client to obtain an access token.
        /// </summary>
        /// <param name="key">Subscription key to use to get an authentication token.</param>
        public AzureAuthToken(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key", "A subscription key is required");
            }

            this.SubscriptionKey = key;
            this.RequestStatusCode = HttpStatusCode.InternalServerError;
        }

        /// <summary>
        /// Gets a token for the specified subscription.
        /// </summary>
        /// <returns>The encoded JWT token prefixed with the string "Bearer ".</returns>
        /// <remarks>
        /// This method uses a cache to limit the number of request to the token service.
        /// A fresh token can be re-used during its lifetime of 10 minutes. After a successful
        /// request to the token service, this method caches the access token. Subsequent 
        /// invocations of the method return the cached token for the next 5 minutes. After
        /// 5 minutes, a new token is fetched from the token service and the cache is updated.
        /// </remarks>
我设置了5分钟重新获取token啊
xdashewan 2017-02-22
  • 打赏
  • 举报
回复
你client.Timeout设了多少,不会是因为timeout了自动关闭吧
xdashewan 2017-02-22
  • 打赏
  • 举报
回复
引用 5 楼 xdashewan 的回复:
是不是你response.EnsureSuccessStatusCode();这句代码发出的?
应该不是,我再看看
xdashewan 2017-02-22
  • 打赏
  • 举报
回复
引用 3 楼 qq_22216299 的回复:
我比较疑惑的是,为什么我的客户端没有等到服务器发application data就关闭了连接呢?是不是因为服务器给我发客户端发送了一个ACK,导致客户端关闭了连接?因为我对比了下,在成功获取token的抓包中,服务器都是先发送了application data,然后发送一个ACK,客户端拆关闭了连接的。
是不是你response.EnsureSuccessStatusCode();这句代码发出的?
qq_22216299 2017-02-22
  • 打赏
  • 举报
回复
引用 2 楼 xdashewan 的回复:
[quote=引用 1 楼 xdashewan 的回复:] 客户端和服务器间的ssl握手,在接到client hello后服务器正常会回复Server Hello,但你的通信中服务器并未作出肯定应答
TCP Previous segment not captured说明包序列乱序了,之后进行Retransmission重发,后面你的客户端也进行应答应该是成功连接上了,之前没看仔细,你的问题是在于你的application data发送后没有等到服务器回发application data就直接发送了关闭连接的FIN[/quote] 我比较疑惑的是,为什么我的客户端没有等到服务器发application data就关闭了连接呢?是不是因为服务器给我发客户端发送了一个ACK,导致客户端关闭了连接?因为我对比了下,在成功获取token的抓包中,服务器都是先发送了application data,然后发送一个ACK,客户端拆关闭了连接的。
qq_22216299 2017-02-22
  • 打赏
  • 举报
回复
我比较疑惑的是,为什么我的客户端没有等到服务器发application data就关闭了连接呢?是不是因为服务器给我发客户端发送了一个ACK,导致客户端关闭了连接?因为我对比了下,在成功获取token的抓包中,服务器都是先发送了application data,然后发送一个ACK,客户端拆关闭了连接的。
xdashewan 2017-02-22
  • 打赏
  • 举报
回复
引用 1 楼 xdashewan 的回复:
客户端和服务器间的ssl握手,在接到client hello后服务器正常会回复Server Hello,但你的通信中服务器并未作出肯定应答
TCP Previous segment not captured说明包序列乱序了,之后进行Retransmission重发,后面你的客户端也进行应答应该是成功连接上了,之前没看仔细,你的问题是在于你的application data发送后没有等到服务器回发application data就直接发送了关闭连接的FIN
xdashewan 2017-02-22
  • 打赏
  • 举报
回复
客户端和服务器间的ssl握手,在接到client hello后服务器正常会回复Server Hello,但你的通信中服务器并未作出肯定应答

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧