2017-06-16 89 views
0
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; 
    } 

} 

我得到了使用Web服務的要求,顯示結果,我試圖通過使用HttpWebRequest類來使用Web服務。我在req.GetResponse()運行異常任何幫助表示讚賞。Web服務動態使用HttpWebRequest不支持的媒體類型

+0

不要清除標題,這可能是什麼問題。 –

+0

好吧,我嘗試一次... –

+0

嗨馬克,我試着通過評論行req.Headers.Clear()行代碼,但仍然是拋出相同的錯誤 –

回答

0

公共無效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; 
     } 

    } 

最後,我發現解決了問題,上面是工作的代碼。我改變後req.ContentType =「application/xml」; req.ContentType =「text/xml; charset = \」UTF-8 \「」; ,req.Accept =「application/xml」; req.Accept =「text/xml」;我刪除了req.Headers.Clear();我的代碼開始工作,感謝所有的支持......

相關問題