runtime icon indicating copy to clipboard operation
runtime copied to clipboard

HttpWebRequest Not Supported HTTP/1.0?

Open milenstack opened this issue 9 years ago • 5 comments

HttpRequest = (HttpWebRequest)WebRequest.CreateHttp(url);
HttpRequest.Method = method;
HttpRequest.ProtocolVersion = System.Net.HttpVersion.Version10;

Parameter 1.0 is specified, but is not valid

Analysis of Using wireshark is still 1.1

milenstack avatar Oct 12 '17 05:10 milenstack

I've been using HttpClient solved

 public async Task<KeyValuePair<Dictionary<string, string>, byte[]>> Post(string url, HttpContent content, Dictionary<string, string> requestHeaders = null)
        {
            try
            {
                Log.Write(new { Url = url });
                var httpRequestMessage = new HttpRequestMessage();
                httpRequestMessage.Version = System.Net.HttpVersion.Version10;
                httpRequestMessage.Method = new HttpMethod("POST");
                httpRequestMessage.RequestUri = new Uri(url);
                httpRequestMessage.Content = content;
                httpClient.DefaultRequestHeaders.Host = (new Uri(url).Host);
                if (requestHeaders != null)
                {
                    foreach (var key in requestHeaders.Keys)
                    {
                        httpRequestMessage.Headers.TryAddWithoutValidation(key, requestHeaders[key]);
                    }
                }
                using (var result = await httpClient.SendAsync(httpRequestMessage))
                {
                    Log.Write(new { Url = url, StatusCode = result.StatusCode });
                    var responseHeaders = new Dictionary<string, string>();
                    foreach (var item in result.Headers)
                    {
                        responseHeaders.Add(item.Key, string.Join(" ", item.Value));
                    }
                    foreach (var item in result.Content.Headers)
                    {
                        responseHeaders.Add(item.Key, string.Join(" ", item.Value));
                    }

                    var responseContext = await result.Content.ReadAsByteArrayAsync();
                    return new KeyValuePair<Dictionary<string, string>, byte[]>(responseHeaders, responseContext);
                }
            }
            catch (Exception ex)
            {
                Log.Write("Http Client Post Exception :" + ex.Message);
                throw ex;
            }
        }

milenstack avatar Oct 12 '17 12:10 milenstack

I've been using HttpClient solved

Are you saying that using HttpClient with HTTP/1.0 works? But that using HttpWebRequest w/ HTTP/1.0 doesn't work?

davidsh avatar Oct 12 '17 14:10 davidsh

@davidsh yes,HttpWebRequest with HTTP/1.0 doesn't work

milenstack avatar Oct 12 '17 17:10 milenstack

@davidsh yes,HttpWebRequest with HTTP/1.0 doesn't work

Can you clarify what .NET Core version you are using? .NET Core 1.0? 1.1? 2.0? Can you attach your Visual Studio solution showing your repro and package dependencies?

davidsh avatar Oct 12 '17 17:10 davidsh

@davidsh .NET Core 2.0 for win 10, the solution is visual studio default template project. you can copy HttpWebRequest ProtocolVersion Sample Tests

https://docs.microsoft.com/zh-cn/dotnet/api/system.net.httpwebrequest.protocolversion?view=netcore-2.0#System_Net_HttpWebRequest_ProtocolVersion

// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
// Use the existing 'ProtocolVersion' , and display it onto the console.	
Console.WriteLine("\nThe 'ProtocolVersion' of the protocol used is {0}",myHttpWebRequest.ProtocolVersion);
// Set the 'ProtocolVersion' property of the 'HttpWebRequest' to 'Version1.0' .
myHttpWebRequest.ProtocolVersion=HttpVersion.Version10;
 // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
 HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
Console.WriteLine("\nThe 'ProtocolVersion' of the protocol changed to {0}",myHttpWebRequest.ProtocolVersion);
Console.WriteLine("\nThe protocol version of the response object is {0}",myHttpWebResponse.ProtocolVersion);

milenstack avatar Oct 12 '17 18:10 milenstack