.net 調用java或者Java調用.net返回的資料轉換問題

來源:互聯網
上載者:User

標籤:

經常發現這類問題,主要是返回的資料轉換問題造成,一般情況下不要直接引用wsdl的方式(如果的調用.Net開發的webservice可以),用HttpWebRequest方式擷取返回的資料,然後再解析,這種方式比引用wsdl成功率高,至於如何傳參數,可以用
SoapUI工具進行分析,能看到該怎樣傳參數
還有用HTTPAnalyzerFull工具可以進行抓包,看看提交和返回的是什麼內容

不管是.net 調用java或者Java調用.net,一般用以上兩個工具都能解決問題

public string GetGsReturn(string url, StringBuilder param)//提交到webservice並返回結果
  {
   string responseString = string.Empty;
   try
   {
    byte[] bs = Encoding.UTF8.GetBytes(param.ToString());

    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
    myRequest.Method = "post";

    myRequest.Headers.Add("SOAPAction", "\"\"");
    myRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
    myRequest.ContentType = "text/xml; charset=utf-8";

    myRequest.KeepAlive = true;
    myRequest.ContentLength = bs.Length;
    myRequest.UserAgent = "Apache-HttpClient/4.1.1 (java 1.5)";

    Stream reqStream = myRequest.GetRequestStream();
    reqStream.Write(bs, 0, bs.Length);

    HttpWebResponse myResponse;
    try
    { myResponse = (HttpWebResponse)myRequest.GetResponse(); }
    catch (WebException ex)
    { myResponse = (HttpWebResponse)ex.Response; }
    if (myRequest != null)
    {
     StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
     responseString = sr.ReadToEnd();//返回的資料
    }
   }
   catch (Exception ex)
   {
    responseString = ex.Message;
   }
   return responseString;
  }

一個參數用例,具體看你的實際需求

 

    StringBuilder param = new StringBuilder();
    param.AppendLine("<?xml version=‘1.0‘ encoding=‘utf-8‘?>");
    param.AppendLine("<soapenv:Envelope xmlns:xsi=‘http://www.w3.org/2001/XMLSchema-instance‘ xmlns:xsd=‘http://www.w3.org/2001/XMLSchema‘ xmlns:soapenv=‘http://schemas.xmlsoap.org/soap/envelope/‘ xmlns:web=‘http://webservice.protal.ems.com‘>");
    param.AppendLine("<soapenv:Header/>");
    param.AppendLine("<soapenv:Body>");

    param.AppendLine("<web:sendSMS soapenv:encodingStyle=‘http://schemas.xmlsoap.org/soap/encoding/‘>");
    param.AppendLine("<enterpriseID xsi:type=‘xsd:string‘>15644</enterpriseID>");
    param.AppendLine("<loginName xsi:type=‘xsd:string‘>admin</loginName>");
    param.AppendLine("<password xsi:type=‘xsd:string‘>511c6269b5eb69538c71ab372529fb1d</password>"); //741123
    param.AppendLine("<smsId xsi:type=‘xsd:string‘></smsId>");
    param.AppendLine("<subPort xsi:type=‘xsd:string‘></subPort>");
    param.AppendLine("<content xsi:type=‘xsd:string‘>" + smstxt + "</content>");
    param.AppendLine("<mobiles xsi:type=‘xsd:string‘>" + tels + "</mobiles>");
    param.AppendLine("<sendTime xsi:type=‘xsd:string‘></sendTime>");
    param.AppendLine("</web:sendSMS>");

    param.AppendLine("</soapenv:Body>");
    param.AppendLine("</soapenv:Envelope>");

    string rtnstr = GetGsReturn(SmsUrl, param);//傳送參數並返回結果

 

.net 調用java或者Java調用.net返回的資料轉換問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.