您的位置:首页 > 编程语言 > Java开发

c#调用带有安全认证的java webservice

2016-05-05 14:43 489 查看
C#调用java带有安全认证的WebService时,自动生成的代理里不包含Header内容,因此需要自己填补这些信息。整理几个调用方法如下:

1. .net framework 3.0以上直接引用服务,通过配置完成,

步骤:在client->endpoint添加headers内容,完整的配置如下:

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

namespace WebServiceClient
{
public class ServiceSoap
{

public static string GJSendSOAPMessage(string url, string soapXml)
{
string responseMsg = String.Empty;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
Encoding myEncoding = Encoding.GetEncoding("utf-8");
string param = HttpUtility.UrlEncode("xml", myEncoding) + "=" + HttpUtility.UrlEncode(soapXml, myEncoding);
byte[] postBytes = Encoding.UTF8.GetBytes(param);
req.ContentLength = postBytes.Length;
try
{
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(postBytes, 0, postBytes.Length);
reqStream.Close();
reqStream.Dispose();
}
using (WebResponse wr = req.GetResponse())//在这里对接收到的页面内容进行处理
{
System.IO.Stream res = wr.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(res);
responseMsg = reader.ReadToEnd(); //返回结果
wr.Close();
}
}
catch (Exception ex)
{
throw ex;
}
return responseMsg;
}

public static void SendSoap()
{
try
{
string url = "http://127.0.0.1:7090/services/test";//?wsdl";
string datastr = GetSoap();
//request
//Uri uri = new Uri(url);
//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(datastr.ToString());
//    requestStream.Write(paramBytes, 0, paramBytes.Length);
//}
////response
//WebResponse webResponse = webRequest.GetResponse();
//using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
//{
//    string result = "";
//    return result = myStreamReader.ReadToEnd();
//}
string res = GJSendSOAPMessage(url, datastr);
Console.WriteLine(res);
}
catch (Exception ex)
{
throw ex;
}

}

public static string GetSoap()
{
StringBuilder soap = new StringBuilder();
soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
soap.Append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");

#region Header
soap.Append("<SOAP-ENV:Header xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">");
soap.Append("<wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" soap:mustUnderstand=\"1\">");
soap.Append("<wsse:UsernameToken wsu:Id=\"UsernameToken - 4c3ba901 - 520b - 4e01 - 931b - 7904df54fd9a\">");
soap.Append("<wsse:Username>用户名</wsse:Username>");
soap.Append("<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">密码</wsse:Password>");
soap.Append("<wsse:Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">Rb+Kmx7s+FapANdzF1r5wg==</wsse:Nonce>");
soap.Append("<wsu:Created>2016-04-28T06:36:52.739Z</wsu:Created>");
soap.Append("</wsse:UsernameToken>");
soap.Append("</wsse:Security>");
soap.Append("</SOAP-ENV:Header>");
#endregion Header end

#region Body

soap.Append("<soap:Body>");
soap.Append("<ns2:transferBidNotice xmlns:ns2=\"http://ws.webservice.psp.sxca.com/\">");
soap.Append("<bidNoticeWS>");
soap.Append("<accordToLaw>1</accordToLaw>");
soap.Append("<agencyOrg>测试代理机构名称</agencyOrg>");
soap.Append("<approveCode>A001</approveCode>");
soap.Append("<area>140100</area>");
soap.Append("<bidOpenTime>2015-12-12T00:00:00.000+08:00</bidOpenTime>");
soap.Append("<bidProjectCode>A001</bidProjectCode>");
soap.Append("<bidProjectName>测试招标项目</bidProjectName>");
soap.Append("<bidSchemeApproveCode>A001</bidSchemeApproveCode>");
soap.Append("<businessId>fca99cef7eb04ebe8b0f84b341b6a3b6</businessId>");
soap.Append("<businessLicence>140500200014001</businessLicence>");
soap.Append("<funding>中央政府资金</funding>");
soap.Append("<industryClassification>A01</industryClassification>");
soap.Append("<investment>123.0</investment>");
soap.Append("<moreAnnexList>");
soap.Append("<annexContent>suLK1Li9vP7E2sjdMQ==</annexContent>");
soap.Append("<annexName>测试附件名称1</annexName>");
soap.Append("</moreAnnexList>");
soap.Append("<moreAnnexList>");
soap.Append("<annexContent>suLK1Li9vP7E2sjdMg==</annexContent>");
soap.Append("<annexName>测试附件名称2</annexName>");
soap.Append("</moreAnnexList>");
soap.Append("<natureId>1</natureId>");
soap.Append("<natureTypeId>1</natureTypeId>");
soap.Append("<noticeContent>测试公告内容</noticeContent>");
soap.Append("<noticeTitle>测试公告标题</noticeTitle>");
soap.Append("<platformBidProjectCode>14000100001</platformBidProjectCode>");
soap.Append("<projectName>测试项目</projectName>");
soap.Append("<releaseType>C</releaseType>");
soap.Append("<resourceType>1</resourceType>");
soap.Append("<tendereeOrg>测试业主名称</tendereeOrg>");
soap.Append("</bidNoticeWS>");
soap.Append("</ns2:transferBidNotice>");
soap.Append("</soap:Body>");
soap.Append("</soap:Envelope>");

#endregion Body End

return soap.ToString();
}

}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: