Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.ArgumentException: The header has an empty value

I am getting an exception as "The header has an empty value". This is the POST request. For 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 Exception in OnRequestResponse callback at System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)this.webRequest.EndGetResponse(ar);

like image 830
nucleons Avatar asked Nov 10 '22 23:11

nucleons


1 Answers

The first thing is, try upgrading your solution to Windows Phone 8.1 There is 2 option: Silverlight 8.1 (you can reuse every code) or WP 8.1 RT (Easily to reuse code between WP and Windows 8.1). I suggest Silverlight 8.1, since you already have Silverlight 8.0 App under development.

Second things: Callback is kinda old. Consider to upgrade your code to Await and Async method, since it is naturally supported

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

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;
    }

Usage:

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

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

like image 61
Tuan Tran Avatar answered Nov 14 '22 23:11

Tuan Tran