Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6065165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:21:37+00:00 2026-05-23T09:21:37+00:00

Here is my simple C# code: using System; using System.Collections.Generic; using System.Text; using System.Net;

  • 0

Here is my simple C# code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {                        
            WebRequest req = WebRequest.Create("http://192.168.1.35:8888/");
            req.Method = "POST";
            req.ContentLength = 0;

            req.Headers.Add("s", "АБВ12");
            req.Headers.Add("username", "user");
            req.Headers.Add("password", "pass");

            System.Net.WebResponse resp = req.GetResponse();            
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            Console.WriteLine(sr.ReadToEnd());
        }
    }
}

So, I trying to send POST request to Apache Server and get server answer. I don’t need any additional request heeaders. The problem is then I tried to run this code I got exception:

System.ArgumentException was unhandled
  Message=Specified value has invalid Control characters.
Parameter name: value
  Source=System
  ParamName=value
  StackTrace:
       at System.Net.WebHeaderCollection.CheckBadChars(String name, Boolean isHeaderValue)
       at System.Net.WebHeaderCollection.Add(String name, String value)
       at Test.Program.Main(String[] args) in D:\Test\Test\Test\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

It seems like I need to convert header value to ISO-8859-1 encoding. So, how can I can get this program working properly? Sorry for my english. I hope for your help.
Thanks in advance!


Sample request that works correctly in my situation:

POST / HTTP/1.1
s: АБВ12
username: user
password: pass
Content-Length: 0
Accept: */*
User-Agent: Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)
Host: 127.0.0.1
Connection: Keep-Alive

UPD
I’ve solve this problem by using Interop component WinHttpRequest:

WinHttp.WinHttpRequest oHTTP = new WinHttp.WinHttpRequest();
oHTTP.Open("POST", "http://127.0.0.1:8888/");
oHTTP.SetRequestHeader("s", args[0]);
oHTTP.SetRequestHeader("username", "user");
oHTTP.SetRequestHeader("password", "pass");
oHTTP.Send();

args[0] contains any cyrillic charachters. Thanks everyone!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:21:37+00:00Added an answer on May 23, 2026 at 9:21 am

    You can use Uri.EscapeDataString to escape the non-ASCII character in the request header. The code below (with a simple WCF service to simulate the receiving side) shows how this can be done. Notice that you’ll also need to unescape the header value at the server side (shown below as well).

    public class StackOverflow_6449723
    {
        [ServiceContract]
        public class Service
        {
            [WebGet(UriTemplate = "*", ResponseFormat = WebMessageFormat.Json)]
            public Stream GetHeaders()
            {
                StringBuilder sb = new StringBuilder();
                foreach (var header in WebOperationContext.Current.IncomingRequest.Headers.AllKeys)
                {
                    sb.AppendLine(string.Format("{0}: {1}", header, Uri.UnescapeDataString(WebOperationContext.Current.IncomingRequest.Headers[header])));
                }
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain; charset=utf-8";
                return new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()));
            }
        }
        public static void Test()
        {
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
            host.Open();
            Console.WriteLine("Host opened");
    
            WebRequest req = WebRequest.Create(baseAddress + "/foo");
            req.Headers.Add("s", Uri.EscapeDataString("АБВ12"));             
            req.Headers.Add("username", "user");
            req.Headers.Add("password", "pass");
            WebResponse resp = req.GetResponse();
            StreamReader sr = new StreamReader(resp.GetResponseStream());
            Console.WriteLine(sr.ReadToEnd()); 
    
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in my HomeController.cs using System; using System.Collections.Generic; using System.Linq;
So here is the simple code: [System.ComponentModel.DefaultValue(true)] public bool AnyValue { get; set; }
Here is a sample code to retrieve data from a database using the yield
I am using the Data Annotation Validator, outlined here: http://www.asp.net/learn/mvc/tutorial-39-cs.aspx The Data Annotations Model
I am using Logging Application block with C#.Net 2.0. My code is logging the
I just made a very simple test app using documentation from MSDN. All I
Here is my sample code: from xml.dom.minidom import * def make_xml(): doc = Document()
Here is my sample code. It is meant to be an iterative procedure for
I'm trying to make things simpler. Here is my code: If Threading.Monitor.TryEnter(syncRoot) Then Try
I'm probably missing something simple here, but I can't find the answer elsewhere. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.