System.Net.WebConnection.GetResponse C# (CSharp) Method

GetResponse() private method

private GetResponse ( byte buffer, int max ) : int
buffer byte
max int
return int
		int GetResponse (byte [] buffer, int max)
		{
			int pos = 0;
			string line = null;
			bool lineok = false;
			bool isContinue = false;
			bool emptyFirstLine = false;
			do {
				if (readState == ReadState.None) {
					lineok = ReadLine (buffer, ref pos, max, ref line);
					if (!lineok)
						return -1;

					if (line == null) {
						emptyFirstLine = true;
						continue;
					}
					emptyFirstLine = false;

					readState = ReadState.Status;

					string [] parts = line.Split (' ');
					if (parts.Length < 2)
						return -1;

					if (String.Compare (parts [0], "HTTP/1.1", true) == 0) {
						Data.Version = HttpVersion.Version11;
						sPoint.SetVersion (HttpVersion.Version11);
					} else {
						Data.Version = HttpVersion.Version10;
						sPoint.SetVersion (HttpVersion.Version10);
					}

					Data.StatusCode = (int) UInt32.Parse (parts [1]);
					if (parts.Length >= 3)
						Data.StatusDescription = String.Join (" ", parts, 2, parts.Length - 2);
					else
						Data.StatusDescription = "";

					if (pos >= max)
						return pos;
				}

				emptyFirstLine = false;
				if (readState == ReadState.Status) {
					readState = ReadState.Headers;
					Data.Headers = new WebHeaderCollection ();
					ArrayList headers = new ArrayList ();
					bool finished = false;
					while (!finished) {
						if (ReadLine (buffer, ref pos, max, ref line) == false)
							break;
					
						if (line == null) {
							// Empty line: end of headers
							finished = true;
							continue;
						}
					
						if (line.Length > 0 && (line [0] == ' ' || line [0] == '\t')) {
							int count = headers.Count - 1;
							if (count < 0)
								break;

							string prev = (string) headers [count] + line;
							headers [count] = prev;
						} else {
							headers.Add (line);
						}
					}

					if (!finished)
						return -1;

					foreach (string s in headers)
						Data.Headers.SetInternal (s);

					if (Data.StatusCode == (int) HttpStatusCode.Continue) {
						sPoint.SendContinue = true;
						if (pos >= max)
							return pos;

						if (Data.request.ExpectContinue) {
							Data.request.DoContinueDelegate (Data.StatusCode, Data.Headers);
							// Prevent double calls when getting the
							// headers in several packets.
							Data.request.ExpectContinue = false;
						}

						readState = ReadState.None;
						isContinue = true;
					}
					else {
						readState = ReadState.Content;
						return pos;
					}
				}
			} while (emptyFirstLine || isContinue);

			return -1;
		}
		

Usage Example

Esempio n. 1
0
        private static void ReadDone(IAsyncResult result)
        {
            WebConnection     webConnection = (WebConnection)result.AsyncState;
            WebConnectionData data          = webConnection.Data;
            Stream            stream        = webConnection.nstream;

            if (stream == null)
            {
                webConnection.Close(sendNext: true);
                return;
            }
            int num = -1;

            try
            {
                num = stream.EndRead(result);
            }
            catch (Exception e)
            {
                webConnection.HandleError(WebExceptionStatus.ReceiveFailure, e, "ReadDone1");
                return;

                IL_004c :;
            }
            if (num == 0)
            {
                webConnection.HandleError(WebExceptionStatus.ReceiveFailure, null, "ReadDone2");
                return;
            }
            if (num < 0)
            {
                webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3");
                return;
            }
            int num2 = -1;

            num += webConnection.position;
            if (webConnection.readState == ReadState.None)
            {
                Exception ex = null;
                try
                {
                    num2 = webConnection.GetResponse(webConnection.buffer, num);
                }
                catch (Exception ex2)
                {
                    ex = ex2;
                }
                if (ex != null)
                {
                    webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, ex, "ReadDone4");
                    return;
                }
            }
            if (webConnection.readState != ReadState.Content)
            {
                int    num3 = num * 2;
                int    num4 = (num3 >= webConnection.buffer.Length) ? num3 : webConnection.buffer.Length;
                byte[] dst  = new byte[num4];
                Buffer.BlockCopy(webConnection.buffer, 0, dst, 0, num);
                webConnection.buffer    = dst;
                webConnection.position  = num;
                webConnection.readState = ReadState.None;
                InitRead(webConnection);
                return;
            }
            webConnection.position = 0;
            WebConnectionStream webConnectionStream = new WebConnectionStream(webConnection);
            string text = data.Headers["Transfer-Encoding"];

            webConnection.chunkedRead = (text != null && text.ToLower().IndexOf("chunked") != -1);
            if (!webConnection.chunkedRead)
            {
                webConnectionStream.ReadBuffer       = webConnection.buffer;
                webConnectionStream.ReadBufferOffset = num2;
                webConnectionStream.ReadBufferSize   = num;
                webConnectionStream.CheckResponseInBuffer();
            }
            else if (webConnection.chunkStream == null)
            {
                try
                {
                    webConnection.chunkStream = new ChunkStream(webConnection.buffer, num2, num, data.Headers);
                }
                catch (Exception e2)
                {
                    webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, e2, "ReadDone5");
                    return;

                    IL_01ef :;
                }
            }
            else
            {
                webConnection.chunkStream.ResetBuffer();
                try
                {
                    webConnection.chunkStream.Write(webConnection.buffer, num2, num);
                }
                catch (Exception e3)
                {
                    webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, e3, "ReadDone6");
                    return;

                    IL_0233 :;
                }
            }
            data.stream = webConnectionStream;
            if (!ExpectContent(data.StatusCode) || data.request.Method == "HEAD")
            {
                webConnectionStream.ForceCompletion();
            }
            data.request.SetResponseData(data);
        }
All Usage Examples Of System.Net.WebConnection::GetResponse