2015-10-15 55 views
0

,而在我的贏窗體應用程序消耗的Java Web服務,我得到錯誤消費的Java Web服務得到錯誤

的錯誤是

,如果你設置你必須提供一個請求主體的ContentLength> 0或 SendChunked ==真。通過在 [開始] GetResponse之前調用[開始] GetRequestStream來完成此操作。

我的代碼來使用Java服務

 public byte[] StringToByteArray(string stringData) 
     { 
     System.Text.UTF8Encoding Encoding = new System.Text.UTF8Encoding(); 
     return Encoding.GetBytes(stringData); 
     } 
     private void button1_Click(object sender, EventArgs e) 
     { 
     string DATA = @"<RepositoryType>117</RepositoryType> 
         <RepositoryCategory>0</RepositoryCategory> 
         <ModifiedBy>2825</ModifiedBy> 
         <ReferenceCode>0</ReferenceCode> 
         <FromDate>2015-10-14T11:50:00</FromDate> 
         <ToDate>2015-10-14T11:51:00</ToDate> 
         <RepositoryName>ashok</RepositoryName> 
         <RepositoryShortName>kumar</RepositoryShortName> 
         <RepositoryDesc>nothing</RepositoryDesc> 
         <Fixed>F</Fixed> 
         <IsValid>true</IsValid> 
         <lstVisa /> 
         <SortOrder>0</SortOrder> 
        </Repository>";` 

        byte[] postdata = null; 
        HttpWebRequest _WebRequest = null; 
     HttpWebResponse webresponse = null; 
     StreamReader ResponseStream = null; 
     string sReturnVal = string.Empty; 
     string 
serviceAddress="http://172.16.12.21:8888/XML_RESPONSE/rest/test/xmltest/"; 


     try 

     { 
      _WebRequest = (HttpWebRequest)WebRequest.Create(serviceAddress + "/" + DATA); 
      postdata = StringToByteArray(DATA); 
      if (_WebRequest != null) 
      { 
       if (postdata!=null) 
       { 


        _WebRequest.Method = "POST"; 

        _WebRequest.ContentType= "text/xml"; 

        _WebRequest.ContentLength = postdata.Length; 

        _WebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"; 
        _WebRequest.SendChunked = true; 



       } 





        **webresponse = (HttpWebResponse)_WebRequest.GetResponse();** 
        { 

          if (webresponse.Headers.Get("Content-Encoding") != null && webresponse.Headers.Get("Content-Encoding").ToLower() == "gzip") 
           ResponseStream = new StreamReader(new GZipStream(webresponse.GetResponseStream(), CompressionMode.Decompress)); 
          else 
          { 
           Encoding enc = System.Text.Encoding.GetEncoding(1252); 
           ResponseStream = new StreamReader(webresponse.GetResponseStream(), enc); 
          } 
          if (ResponseStream != null) 
          { 
           XElement Root = XElement.Load(ResponseStream); 
           sReturnVal = Root.Value; 
          } 

        } 

      } 
      else 
      { 
       throw new Exception("Connection to " + " Service could not be Established.", 
        new Exception("Please Check whether " + 
         " Service is running Or Contact your System Administrator.")); 

      } 
     } 

     catch(Exception ex) 

     { 

     } 
    } 

突出顯示的行正變得錯誤。

請幫忙。

+0

歡迎來到Stack Overflow。我已經修復了您的帖子中的一些英文問題。我也修正了代碼的降價。但是我看不到你突出顯示的那一行。 –

回答

0

您正在將數據添加到URL中,而不是將其作爲請求正文發佈。看看你可以使用的工作代碼的這個問題:HTTP POST using web service。使用ASP.NET webservices,您必須設置SOAPAction HTTP標頭,如果您的服務不需要它,則可以跳過此行。