[英]TCP connection phase fails (calling web service from JAVA client to C#-server)
因此我有一個適用於Android的應用程序,該應用程序調用了Web服務(C#),從一開始我就注意到它經常但並非始終如此,返回答案的速度非常慢,或者請求完全失敗。
起初,我認為這與后端代碼有關,即服務器需要很長時間來處理請求並返回結果。 我現在排除了這種可能性,因為處理只需要40毫秒。 但是,我確實注意到有時不調用Web服務代碼,或者執行代碼需要花費很長時間。
因此,我啟動了Wireshark,查看了傳輸的數據包,發現了我認為錯誤的TCP連接階段。 下面的第一張圖片顯示了錯誤的連接。
看起來像這樣:
如下圖所示:
但是它應該看起來像這樣:
當我再次嘗試時,只要再次從Android應用程序中調用webservie(不重新啟動或執行任何操作),我將獲得正確的步驟,並得到預期的響應。 這是來自其中的Wireshark圖像:
我非常確定這是我認為“非常緩慢的Web服務”的問題,因為在收到答案之前通常會有很長的延遲,有時(如第一張圖片所示)根本沒有響應。 Wireshark還顯示,它只能在10-20秒內“拖延”答案,而這實際上是在重新獲得Web服務代碼之前。
沒有網絡問題,因為所有問題都在本地WLAN上運行(計算機充當熱點,沒有其他人與其連接)。 Android設備是運行2.3.3的Samsung S2,Web服務是運行自托管ServiceHost的.NET 3.5。
有任何想法嗎?
- - - - 附加信息 - - - - - -
這是我從JAVA代碼(方法是GET)中調用Web服務的方式:
private void executeRequest(HttpUriRequest request, String url)
{
HttpClient client = new DefaultHttpClient();
try
{
httpResponse = client.execute(request);
responseCode = httpResponse.getStatusLine().getStatusCode();
message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
response = convertStreamToString(instream);
// Closing the input stream will trigger connection release
instream.close();
}
} catch (ClientProtocolException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
} catch (IOException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
}
}
這是在.NET中定義Web服務方法的方式:
[OperationContract]
[WebGet(UriTemplate = "PutMessage?jsonString={jsonString}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat= WebMessageFormat.Json)]
string PutMessage(string jsonString);
如果您更仔細地查看轉儲,您實際上看到的是客戶端沒有從服務器獲取任何數據包。
Client: SYN
Server: SYN ACK (ACK, never reaches the client)
Server: SYN ACK (Retransmission of the first ACK, also lost. )
Client: SYN (Retransmission, it does not know the server received the first SYN)
Server: SYN ACK (ACK on the SYN from the client, once again lost)
Client: SYN (Retransmission since it *still* does not know the server got the SYN)
換句話說,如果客戶端未接收到任何服務器數據包,則從雙方進行的完全正常的重新傳輸。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.