System.ArgumentException: header is empty

I get an exception like "The header is empty." This is a POST request. In some cases, I need to send some header value as empty.

This is my code:

private void Start() { try { this.webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(this.requestURL); //this.webRequest.Headers[HttpRequestHeader.UserAgent] = this.userAgent; webRequest.Headers[HttpRequestHeader.CacheControl] = "no-cache"; webRequest.Headers[HttpRequestHeader.Pragma] = "no-cache"; webRequest.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString(); webRequest.Method = "POST"; if (headers != null && headers.Count > 0) { webRequest.Headers["some-Header"] = ""; } IAsyncResult result = this.webRequest.BeginGetRequestStream(new AsyncCallback(RequestCallback), webRequest); } catch (Exception ex) { } } void RequestCallback(IAsyncResult result) { HttpWebRequest request = (HttpWebRequest)result.AsyncState; //if (RequestBody != null) { using (Stream postStream = request.EndGetRequestStream(result)) { using (var writer = new StreamWriter(postStream)) { writer.Write(string.Empty); writer.Flush(); postStream.Position = 0; byte[] mArray = new byte[postStream.Length]; postStream.Read(mArray, 0, (int)postStream.Length); } } } request.BeginGetResponse(new AsyncCallback(OnRequestResponse), request); } private void OnRequestResponse(IAsyncResult ar) { try { System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)this.webRequest.EndGetResponse(ar); webResponseHeaders = response.Headers.GetHeaders(); System.IO.Stream responseStream = response.GetResponseStream(); } catch (Exception ex) { } } 

Getting an exception in the OnRequestResponse callback in System.Net.HttpWebResponse response = (System.Net.HttpWebResponse) this.webRequest.EndGetResponse (ar);

+5
source share
1 answer

First of all, try upgrading your solution to Windows Phone 8.1. There are 2 options: Silverlight 8.1 (you can reuse each code) or WP 8.1 RT (easy to use code between WP and Windows 8.1). I suggest Silverlight 8.1, since you already have a Silverlight 8.0 application.

Second: The call back is kind. Consider upgrading your code to Await and Async, as it is naturally supported.

For your problems, try using HttpClient to send the request. Here is a sample code:

 public static async Task<string> GetHttpAsStringTask(string uriString) { string result; Uri targetUri = new Uri(uriString); HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, targetUri); //Add your empty header here request.Headers.Add("Header name","header value"); HttpResponseMessage response = await client.SendAsync(request); using (Stream responseStream = await response.Content.ReadAsStreamAsync()) { StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); result = reader.ReadToEnd(); } return result; } 

Using:

 string response = await GetHttpAsStringTask("your url"); 

for waiting and async in WP 8: http://developer.nokia.com/community/wiki/Asynchronous_Programming_For_Windows_Phone_8

+1
source

Source: https://habr.com/ru/post/1206558/


All Articles