Search code examples
c#xmlweb-servicessystem.net.httpwebrequest

Web Service dynamically using HttpWebRequest Unsupported Media Type


public void processVoucher()
{
    try
    {
        string url = "http://192.168.xxx.xx:xxxx/context-root-xxxxxxxx/AccountsPayableManagerPort?WSDL/processVoucher"; 
        StreamReader str = new StreamReader(@"F:\IntelliChief integration to JD Edwards for AP Invoice entry\processVoucher_input_payload.xml");
        string ipParameter = str.ReadToEnd();

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.ContentType = "application/xml";
        req.KeepAlive = true;
        req.Timeout = 30000;
        req.Accept = "application/xml";//"text/xml";
        req.Headers.Clear();
        req.Method = "POST";
        Encoding encode = Encoding.GetEncoding("utf-8");


        using (Stream stm = req.GetRequestStream())
        {
            using (StreamWriter stmw = new StreamWriter(stm))
            {
                stmw.Write(ipParameter);
            }
        }

        var response = req.GetResponse(); // here i am getting Unsupported Media Type issue
        Stream responseStream = response.GetResponseStream();
        StreamReader strReader = new StreamReader(responseStream, encode, true);
        string result = strReader.ReadToEnd();
    }
    catch (Exception ex)
    {

        MessageBox.Show("Error Message:" + ex.Message);
        throw;
    }

}

I got requirement of consuming web service, display the result, i am trying to consume web service by using HttpWebRequest class. I running exception in req.GetResponse() any help is appreciated.


Solution

  • public void processVoucher() {

            string soap = null;
    
            try
            {
                StreamReader str = new StreamReader(@"F:\xxx\some.xml");
                soap = str.ReadToEnd();
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://192.168.xxx.xx:xxxx/bla-bla-bla/AccountsPayableManagerPort?WSDL");
                req.ContentType = "text/xml;charset=\"UTF-8\"";
                req.Accept = "text/xml";
                req.Method = "POST";
    
                using (Stream stm = req.GetRequestStream())
                {
                    using (StreamWriter stmw = new StreamWriter(stm))
                    {
                        stmw.Write(soap);
                    }
                }
    
    
                using (WebResponse response = req.GetResponse())
                {
                    using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                    {
                        string soapResult = rd.ReadToEnd();
    
                    }
                }
            }
            catch (Exception)
            {
    
                throw;
            }
    
        }
    

    Finally i found solution to the issue, above is the working code. After i changed req.ContentType = "application/xml"; to req.ContentType = "text/xml;charset=\"UTF-8\""; , req.Accept = "application/xml"; to req.Accept = "text/xml"; and i removed req.Headers.Clear(); my code started working thanks all for your support...