1
回答
java webservice setOperationName对应c#的什么方法
注册华为云得mate10,2.9折抢先购!>>>   

有下面一段java代码,需要把他改成c#的,但是Java里的setOperationName不知道c#怎么写,有懂的麻烦告知下,谢谢

Java代码

String endpoint = "http://k5.kingtrans.cn/services/WebCOrderServlet";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTimeout(new Integer(300000));
call.setTargetEndpointAddress(new java.net.URL(endpoint));
// 调用的方(addDJCCorder or addYBCorder or getChannel or getOrderPrintLabel)
call.setOperationName("xxx");
// 提交的内容为 xml 格式字符串
String str = (String) call.invoke(new Object[] { 提交的内容 });
call = null;
service = null;

c#代码

//发起请求
Uri uri = new Uri("http://k5.kingtrans.cn/services/WebCOrderServlet?Wsdl");
WebRequest webRequest = WebRequest.Create(uri);
webRequest.ContentType = "text/xml; charset=utf-8";
webRequest.Method = "POST";
using (Stream requestStream = webRequest.GetRequestStream())
{
    byte[] paramBytes = Encoding.UTF8.GetBytes(webServiceGetRequest());
    requestStream.Write(paramBytes, 0, paramBytes.Length);
}

//响应
WebResponse webResponse = webRequest.GetResponse();
using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
    Console.WriteLine(myStreamReader.ReadToEnd());
}

 

举报
yxmsw2007
发帖于6个月前 1回/37阅

问题已解决,C#需要用自己组装内容,没有现成方法,代码如下

private void webservice_Click(object sender, EventArgs e)
        {
            //string city = "深圳";
            //WeatherWebService ws = new WeatherWebService();
            //string[] r = ws.getWeatherbyCityName(city);
            //if (r == null)
            //{
            //    this.responseshipment.Text = "无" + city + "城市的天气信息";
            //    return;
            //}
            //foreach (string i in r)
            //{
            //    this.responseshipment.Text += i;
            //}

            String URL = "http://k5.kingtrans.cn/services/WebCOrderServlet?Wsdl";
            //String method = methodGetOrderPrintLabel;
            String method = methodGetChannel;

            WebRequest webRequest = WebRequest.Create(URL);
            HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
            httpRequest.ProtocolVersion = HttpVersion.Version10;
            httpRequest.Method = "POST";
            httpRequest.ContentType = "text/xml; charset=utf-8";
            httpRequest.Accept = "application/soap+xml, application/dime, multipart/related, text/*";
            httpRequest.UserAgent = "Axis/1.4";
            httpRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
            httpRequest.Headers.Add("SOAPAction", "\"\"");
            httpRequest.Timeout = 300000;
            httpRequest.Credentials = CredentialCache.DefaultCredentials;
            Stream requestStream = httpRequest.GetRequestStream();
            //Create Stream and Complete Request             
            StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.GetEncoding("utf-8"));
            
            streamWriter.Write(webServiceGetRequest(method));
            streamWriter.Close();

            HttpWebResponse res;
            try
            {
                res = (HttpWebResponse)httpRequest.GetResponse();
            }
            catch (WebException ex)
            {
                res = (HttpWebResponse)ex.Response;
            }
            // StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
            StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("utf-8"));
            string strHtml = sr.ReadToEnd();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(strHtml);

            string result = HtmlDecode(doc.OuterXml);
            Console.WriteLine(result);
            // this.responseshipment.Text = HtmlDecode(doc.OuterXml);
            this.responseshipment.Text = result;
            
        }

        private string getK5RequestContent(string method)
        {
            StringBuilder result = new StringBuilder();

            if (methodGetChannel.Equals(method))
            {
                result.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                result.Append("<CreateAndPreAlertOrderService>");
                result.Append("<authtoken>g4Diw0qLv52JiHEulb7k</authtoken>");
                result.Append("<clientid>alans</clientid>");
                result.Append("</CreateAndPreAlertOrderService>");
            }
            else if (methodGetOrderPrintLabel.Equals(method))
            {
                result.Append("<?xml version= \"1.0\" encoding= \"utf-8\"?>");
                result.Append("<GeOrdertPrintServiceRequest>");
                result.Append("<authtoken>g4Diw0qLv52JiHEulb7k</authtoken>");
                result.Append("<clientid>alans</clientid>");
                result.Append("<corpbillid>");
                result.Append("EYT2014111229913SZ,EYT2014111129860SZ,EYT2014111129857SZ,");
                result.Append("</corpbillid>");
                result.Append("<url>http://k5.kingtrans.cn/</url>");
                result.Append("<paper>100-100</paper>");
                result.Append("<content>1,2,3</content>");
                result.Append("<ifprinttime>1</ifprinttime>");
                result.Append("</GeOrdertPrintServiceRequest>");
            }
            else if (methodGetReOrderBillid.Equals(method))
            {
                result.Append("<?xml version= \"1.0\" encoding= \"utf-8\"?><ChannelAndCorpBillidServiceRequest>");
                result.Append("<authtoken>g4Diw0qLv52JiHEulb7k</authtoken>");
                result.Append("<clientid>alans</clientid>");
                result.Append("<ChannelAndCorpBillidArray>");
                result.Append("<corpbillid>EYT2014111229913SZ</corpbillid>");
                result.Append("<channelid>CNPOST</channelid>");
                result.Append("</ChannelAndCorpBillidArray>");
                result.Append("<ChannelAndCorpBillidArray>");
                result.Append("<corpbillid>EYT2014111129860SZ</corpbillid>");
                result.Append("<channelid>DHL</channelid>");
                result.Append("</ChannelAndCorpBillidArray>");
                result.Append("</ChannelAndCorpBillidServiceRequest>");
            }
            else if (methodAddDJCCorder.Equals(method) || methodAddYBCorder.Equals(method))
            {
                result.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                result.Append("<CreateAndPreAlertOrderService>");
                result.Append("<authtoken>g4Diw0qLv52JiHEulb7k</authtoken>");
                result.Append("<clientid>alans</clientid><CreateAndPreAlertOrderRequestArray>");
                result.Append("<CreateAndPreAlertOrderRequest>");
                result.Append("<billid>EE80000471001CN</billid>");
                result.Append("<refernumb>REF201411080001</refernumb>");
                result.Append("<channelid>DHL</channelid>");
                result.Append("<recname>Test</recname>");
                result.Append("<recaddr>301-311 Barbu Vacarescu str, LAKEVIEW Bld, 1st Floor</recaddr>");
                result.Append("<rectel>40-722-302101</rectel>");
                result.Append("<reccorp>ShenZhen City Of XXX company</reccorp>");
                result.Append("<reccity>ShenZhen</reccity>");
                result.Append("<recpost>518000</recpost>");
                result.Append("<recemail>Zs124@foxmail.com</recemail>");
                result.Append("<country>CN</country>");
                result.Append("<DeclareInvoiceArray>");
                result.Append("<DeclareInvoice>");
                result.Append("<itemcont>Clothes</itemcont>");
                result.Append("<itemcustoms>衣服</itemcustoms>");
                result.Append("<itempeihuo>蓝色衣服 L 码</itempeihuo>");
                result.Append("<itemvalue>2.11</itemvalue>");
                result.Append("<itemnum>10</itemnum>");
                result.Append("<itemprodno>1001</itemprodno>");
                result.Append("<itemphnote>蓝色衣服 L 码</itemphnote>");
                result.Append("<itemunit>USD</itemunit>");
                result.Append("<itemsbprice>2.21</itemsbprice>");
                result.Append("<itemimgurl>");
                result.Append("http://g04.a.alicdn.com/kf/UT8khaXXcpaXXagOFbXK.jpg_50x50.jpg");
                result.Append("</itemimgurl>");
                result.Append("</DeclareInvoice>");
                result.Append("</DeclareInvoiceArray>");
                result.Append("</CreateAndPreAlertOrderRequest>");
                result.Append("</CreateAndPreAlertOrderRequestArray>");
                result.Append("</CreateAndPreAlertOrderService>");
            }
            return HtmlEncode(result.ToString());
        }

        private string webServiceGetRequest(string method)
        {
            StringBuilder result = new StringBuilder();
            
            result.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            result.Append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
            result.Append("<soapenv:Body>");
            result.Append("<" + method + " soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">");
            result.Append("<arg0 xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">");

            result.Append(getK5RequestContent(method));
            
            result.Append("</arg0>");
            result.Append("</" + method +">");
            result.Append("</soapenv:Body>");
            result.Append("</soapenv:Envelope>");
            
            return result.ToString();
        }

        ///<summary>
        ///替换html中的特殊字符
        ///</summary>
        ///<paramname="theString">需要进行替换的文本。</param>
        ///<returns>替换完的文本。</returns>
        public static string HtmlEncode(string theString)
        {
            theString=theString.Replace(">","&gt;");
            theString=theString.Replace("<","&lt;");
            // theString=theString.Replace(" ","&nbsp;");
            theString=theString.Replace("\"","&quot;");
            theString = theString.Replace("\'", "&#39;");
            theString=theString.Replace("\n","<br/>");
            return theString;
        }

        ///<summary>
        ///恢复html中的特殊字符
        ///</summary>
        ///<paramname="theString">需要恢复的文本。</param>
        ///<returns>恢复好的文本。</returns>
        public static string HtmlDecode(string theString)
        {
            theString=theString.Replace("&gt;",">");
            theString=theString.Replace("&lt;","<");
            theString=theString.Replace("&nbsp;"," ");
            theString=theString.Replace("&quot;","\"");
            theString = theString.Replace("&#39;", "\'");
            theString=theString.Replace("<br/>","\n");
            return theString;
        }

 

顶部