调用web服务上传文件
Posted xuexiaodong2009
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调用web服务上传文件相关的知识,希望对你有一定的参考价值。
public string UploadImage46Inner(string adm, string EmrCode, List<string> tmpImgName, List<string> localFilePaths)
string textResult = string.Empty;
ReturnResult<string> rs = new ReturnResult<string>();
string datastream = string.Empty;
string url = WebServiceURI + "/NurMp.Service.WebService.GetData.cls";
string filenames = string.Empty;
try
System.Diagnostics.Debug.Assert(localFilePaths.Count>0);
System.Diagnostics.Debug.Assert(tmpImgName.Count==localFilePaths.Count);
char splitChar = '^';
for (int i = 0; i < tmpImgName.Count; i++)
filenames += (tmpImgName[i]) + splitChar;
filenames = filenames.TrimEnd(splitChar);
DateTime dt = DateTime.Now;
string createDT = string.Format("0:s", dt) + "Z";
string expiresDT = string.Format("0:s", dt.AddMinutes(5)) + "Z";
HttpWebRequest request = System.Net.WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = @"multipart/related; type=""text/xml""; boundary=--boundary2629.3529411764705883531.411764705882353--";
request.Headers.Add("SOAPAction", "http://tempuri.org/NurMp.Service.WebService.GetData.UploadAscii");//NAMESPACE 方法名称
StringBuilder xmlsb = new StringBuilder();
xmlsb.AppendLine("----boundary2629.3529411764705883531.411764705882353--");
xmlsb.AppendLine("Content-Type:text/xml;charset=\\"UTF-8\\"");
xmlsb.AppendLine("Content-Transfer-Encoding:8bit");
xmlsb.AppendLine("");
xmlsb.AppendLine("<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>");
xmlsb.AppendLine("<soap:Envelope xmlns:soap=\\"http://schemas.xmlsoap.org/soap/envelope/\\" xmlns:xsi=\\"http://www.w3.org/2001/XMLSchema-instance\\" xmlns:xsd=\\"http://www.w3.org/2001/XMLSchema\\" xmlns:wsa=\\"http://schemas.xmlsoap.org/ws/2004/08/addressing\\" 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\\">");
xmlsb.AppendLine("<soap:Header>");
xmlsb.AppendLine("<wsa:Action>http://tempuri.org/NurMp.Service.WebService.GetData.UploadAscii</wsa:Action>");//方法名称 NAMESPACE
xmlsb.AppendLine("<wsa:MessageID>urn:uuid:bd1af25d-df1d-4058-9ad8-e56c3faa7136</wsa:MessageID>");
xmlsb.AppendLine("<wsa:ReplyTo>");
xmlsb.AppendLine("<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>");
xmlsb.AppendLine("</wsa:ReplyTo>");
xmlsb.AppendLine("<wsa:To>" + url + "</wsa:To>");//web服务地址
xmlsb.AppendLine("<wsse:Security soap:mustUnderstand=\\"1\\">");
xmlsb.AppendLine("<wsu:Timestamp wsu:Id=\\"Timestamp-871a5192-e606-41f9-878e-a5df753974de\\">");
xmlsb.AppendLine("<wsu:Created>" + createDT + @"</wsu:Created>");
xmlsb.AppendLine("<wsu:Expires>" + expiresDT + @"</wsu:Expires>");
xmlsb.AppendLine("</wsu:Timestamp>");
xmlsb.AppendLine("<wsse:UsernameToken xmlns:wsu=\\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\\" wsu:Id=\\"SecurityToken-0b0cdade-c74e-4f43-b4e1-81e9af382c35\\">");
xmlsb.AppendLine("<wsse:Username>" + this.username + "</wsse:Username>");//web服务用户名
xmlsb.AppendLine("<wsse:Password Type=\\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\\">" + this.password + "</wsse:Password>");///web服务用户密码
xmlsb.AppendLine("<wsse:Nonce>sNGeAirMJGFj4vu5aIwuzw==</wsse:Nonce>");
xmlsb.AppendLine("<wsu:Created>" + createDT + @"</wsu:Created>");
xmlsb.AppendLine("</wsse:UsernameToken>");
xmlsb.AppendLine("</wsse:Security>");
xmlsb.AppendLine("</soap:Header>");
xmlsb.AppendLine("<soap:Body>");
xmlsb.AppendLine("<UploadAscii xmlns=\\"http://tempuri.org\\">");///web服务方法
xmlsb.AppendLine("<filename>" + filenames + "</filename>");///web服务方法参数
xmlsb.AppendLine("<Adm>" + adm + "</Adm>");///web服务方法参数
xmlsb.AppendLine("<EmrCode>" + EmrCode + "</EmrCode>");///web服务方法参数
xmlsb.AppendLine("</UploadAscii>");
xmlsb.AppendLine("</soap:Body>");
xmlsb.AppendLine("</soap:Envelope>");
string boundary = xmlsb.ToString();
using (Stream stream = request.GetRequestStream())
byte[] part = Encoding.UTF8.GetBytes(boundary);
stream.Write(part, 0, part.Length);
for (int i = 0; i < localFilePaths.Count; i++)//上传附件
string boundary2 = @"
----boundary2629.3529411764705883531.411764705882353--
Content-Transfer-Encoding:binary
Content-Type:application/octet-stream
";
byte[] part2 = Encoding.UTF8.GetBytes(boundary2);
stream.Write(part2, 0, part2.Length);
FileInfo fi = new FileInfo(localFilePaths[i]);
using (FileStream fs = fi.OpenRead())
byte[] fileBytepdf = new byte[fs.Length];
fs.Read(fileBytepdf, 0, fileBytepdf.Length);
stream.Write(fileBytepdf, 0, fileBytepdf.Length);
string boundary4 = @"
----boundary2629.3529411764705883531.411764705882353--
";
byte[] part4 = Encoding.UTF8.GetBytes(boundary4);
stream.Write(part4, 0, part4.Length);
using (WebResponse response = request.GetResponse())
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
string result = rd.ReadToEnd();
XmlDocument xmlDoc = new XmlDocument();
//为文档导入数据
xmlDoc.LoadXml(result);
LogInfo.Debug("调用web服务上传图片返回数据:result=" + result);
datastream = xmlDoc.InnerText;
if(!string.IsNullOrEmpty(datastream))
rs = JsonConvert.DeserializeObject<ReturnResult<string>>(datastream);
else
LogInfo.Error("调用web服务上传图片返回为空:url=" + url+ ",filenames="+ filenames);
throw new Exception("调用web服务上传图片返回为空");
catch (Exception ex)
LogInfo.Error("调用web服务上传图片返回错误:" + datastream+":url = " + url+ ", filenames = "+ filenames, ex);
throw new Exception(ex.Message);
if (!rs.IsOK())
LogInfo.Error("调用web服务上传图片返回错误:" + datastream + ":url = " + url + ", filenames = " + filenames);
textResult = rs.msg;
return textResult;
return textResult;
以上是关于调用web服务上传文件的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Retrofit 在 android 中调用图像上传 Web 服务 (ASP.NET) .asmx 文件