コード例 #1
0
        private static void ProcessNetworkResponse(Delegate del, Delegate netOpInProgressDel)
        {
            if (isRequestInProgress == false)
            {
                return;
            }

            SocketAsyncEventArgs socketReceiveEventArg = new SocketAsyncEventArgs();

            socketReceiveEventArg.RemoteEndPoint = endPoint;
            socketReceiveEventArg.Completed     += new EventHandler <SocketAsyncEventArgs>(delegate(object o, SocketAsyncEventArgs e)
            {
                if (e.SocketError != SocketError.Success)
                {
                    // signal that a network operation is done and unsuccessful
                    netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);

                    // clean up the socket
                    CleanupSocket();

                    return;
                }

                // response was received
                int num = e.BytesTransferred;
                if (num > 0)
                {
                    // get the response as a string
                    string resp = Encoding.UTF8.GetString(e.Buffer, 0, num);

                    string[] lines = resp.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                    if (lines == null || lines.Length < 2 ||
                        lines[0] != "HTTP/1.1 200 OK")
                    {
                        // signal that a network operation is done and unsuccessful
                        netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);

                        // clean up the socket
                        CleanupSocket();
                    }

                    // signal that a network operation is done and successful
                    netOpInProgressDel.DynamicInvoke(false, OperationStatus.Success);

                    // discover the content type (default to json)
                    string contentType = "application/json";
                    foreach (var line in lines)
                    {
                        if (line.StartsWith("Content-Type:"))
                        {
                            string compositeContentType = line.Split(':')[1];
                            contentType = compositeContentType.Split(';')[0].Trim();
                            break;
                        }
                    }

                    // get a stream over the last component of the network response
                    MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(lines[lines.Length - 1]));

                    // deserialize the response string
                    HttpMessageBodyWrapper <string> body =
                        (HttpMessageBodyWrapper <string>)HttpWebResponseWrapper <string> .DeserializeResponseBody(
                            stream, contentType, typeof(HttpMessageBodyWrapper <string>));

                    // signal that a network operation is done and successful
                    netOpInProgressDel.DynamicInvoke(false, OperationStatus.Success);

                    // reset the request in progress flag
                    isRequestInProgress = false;

                    // * leave the socket open for a potential next transaction *
                    // CleanupSocket();

                    // invoke the delegate passed in with the actual response text to return to the caller
                    del.DynamicInvoke(body == null ? "" : body.Value);
                }
                else
                {
                    // signal that a network operation is done and unsuccessful
                    netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);

                    // clean up the socket
                    CleanupSocket();
                }
            });

            // set the receive buffer
            byte[] buffer = new byte[32768];
            socketReceiveEventArg.SetBuffer(buffer, 0, buffer.Length);

            // receive the response
            try
            {
                bool ret = socket.ReceiveAsync(socketReceiveEventArg);
                if (ret == false)
                {
                    // signal that a network operation is done and unsuccessful
                    netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);

                    // clean up the socket
                    CleanupSocket();
                }
            }
            catch (Exception ex)
            {
                // trace network error
                TraceHelper.AddMessage("ProcessNetworkResponse: ex: " + ex.Message);

                // signal that a network operation is done and unsuccessful
                netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);

                // clean up the socket
                CleanupSocket();
            }
        }
コード例 #2
0
ファイル: HttpWrappers-ios.cs プロジェクト: ogazitt/zaplify
 // deserialize the status code out of the message body, and reset the stream
 private void DeserializeMessageBody()
 {
     // get the status code out of the response
     bodyWrapper = (HttpMessageBodyWrapper <T>)DeserializeResponseBody(innerResponse, typeof(HttpMessageBodyWrapper <T>));
 }