.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> WCF >> Post New QuestionBookmark and Share Subscribe to Forum

Post and Get File using Webservice

Posted By: kamlesh     Posted Date: August 28, 2013    Points:5   Category :WCF
How to Post zip file from machine to webservice and how to get that zip file. how to do please help me

Responses
Author: kamlesh             
Posted Date: August 28, 2013     Points: 20   

Here
1) UserName,
2) OSType,
3) Files = Folder Path Which u want to Upload
These are In Parameter:

string boundary = Guid.NewGuid().ToString();
HttpWebRequest request = HttpWebRequest.Create("https://" + strServerIPAddress + "/sadmin/setagentversion")
as HttpWebRequest;
request.Method = "POST";
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
request.PreAuthenticate = true;
request.AllowWriteStreamBuffering = true;

if (NoProxy == 1)
{
}
else if (WebBrowserProxy == 1)
{
//setup the proxy
request.Proxy = new WebProxy(ProxyIP, Port);
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
request.Proxy.Credentials = new NetworkCredential(ProxyUsername, ProxyPassword);
}
else if (ManualProxy == 1)
{
request.Proxy = new WebProxy(ProxyIP, Port);
request.Proxy.Credentials = new NetworkCredential(ProxyUsername, ProxyPassword);
}

StringBuilder sb = new StringBuilder();
sb.AppendFormat("--{0}", boundary);
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Disposition: form-data; name=\"Files\"; filename=\"" + Path.GetFileName(this.Files) + "\"");
sb.AppendFormat("\r\n");
sb.AppendFormat(this.Files);
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);

using (FileStream fs = new FileStream(this.Files, FileMode.Open, FileAccess.Read))
{
byte[] contents = new byte[fs.Length];
fs.Read(contents, 0, contents.Length);
sb.Append(Encoding.Default.GetString(contents));
}

sb.AppendFormat("\r\n");

sb.AppendFormat("--{0}", boundary);
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Disposition: form-data; name=\"UserName\"");
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
sb.AppendFormat(this.UserName);
sb.AppendFormat("\r\n");

sb.AppendFormat("--{0}", boundary);
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Disposition: form-data; name=\"OSType\"");
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
sb.AppendFormat(this.OSType);
sb.AppendFormat("\r\n");

sb.AppendFormat("--{0}--", boundary);
byte[] fulldata = Encoding.Default.GetBytes(sb.ToString());
request.ContentLength = fulldata.Length;

request.Timeout = 10000000;
using (Stream sw = request.GetRequestStream())
{
sw.Write(fulldata, 0, fulldata.Length);
}

HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
XmlDocument xmldoc = new XmlDocument();
XmlDataDocument objXMLDocument = new XmlDataDocument();

StreamReader StreamReader = new StreamReader(response.GetResponseStream());
string strReaderXML = StreamReader.ReadToEnd();
StringReader srtrdr = new StringReader(strReaderXML);
ReadXMLFile(strReaderXML);
}


Thanks And Regards.
Kamlesh Nikam


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend