none
Invalid Request from Web API to a Console Application using HttpListener RRS feed

  • Question

  • An ASP.NET Web API tries to communicate with a console Application on Another Machine in LAN with a Combination of IP Address and PORT where the particular Console App is listening for incoming requests. But I get a Http Error of Bad Request. The request Hostname is Invalid Error. I am not sure why this occuring. I gave permissions for that URL using netsh on machine where Console Application is listening.

    Here is the Web API code trying to communicate with Console App

    HttpWebRequest toClient = (HttpWebRequest)WebRequest.Create("http://" + GetRemoteIP(Msg) + ":10000");
                toClient.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                toClient.Accept = "application/json, text/javascript, */*; q=0.01";
                toClient.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36";
                String DatatoClient = "Hello";
                toClient.Method = "POST";

    Similarly on the Console Application I have the following code:

    listener = new HttpListener();
                listener.Prefixes.Add("http://localhost:10000/");
                Console.WriteLine("HttpProcessor in Proxy Started and Accepting Connections.......");
                listener.Start();
                HttpListenerContext ctx = listener.GetContext();
                var reader = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding);
                String data = reader.ReadToEnd();
                //Extract the Account and Amount from the Http Request
                Console.WriteLine("Data from HttpReq from API:{0}",data );
    
                if (data == "hello")
                {
                    rstr += "Yes";
                }
                else
                {
                    rstr += "No";
                }
                byte[] buf = Encoding.UTF8.GetBytes(rstr);
                ctx.Response.ContentLength64 = buf.Length;
                ctx.Response.OutputStream.Write(buf, 0, buf.Length);
                ctx.Response.OutputStream.Close();
                listener.Stop();
                listener.Close();

    I gave the netsh permissions for the Port on remote machine where console App resides with following command

    netsh> http add urlacl url=http://localhost:10000/ user=everyone

    But I don't receive any request on my Console APp instead I receive the above mentioned error on Web API. Why is it so? am I missing something?


    K.V.N.PAVAN

    Thursday, June 19, 2014 12:21 AM

Answers

  • Hi,

    This forum is used to discuss the question about the Windows Communication Foundation, Serialization, and Networking, then for your question it will be better for you to post it in the Web API fourm, then you will get good replies from the Web API experts.

    #Web API Forum:
    http://forums.asp.net/1246.aspx/1?Web+API

    Thanks for your understanding.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 19, 2014 6:24 AM
    Moderator