此代碼的問題在於,文件一旦上傳,就不是正確的格式。我正在嘗試上傳.zip文件。使用C#中的POST將文件上傳到Web服務
公共字符串HttpPost(URI字符串,字符串參數) {
的WebRequest的WebRequest = WebRequest.Create(URI);
NetworkCredential credentials = new NetworkCredential("username", "password");
webRequest.Credentials = credentials;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(parameter);
Stream os = null;
try
{ // send the Post
webRequest.ContentLength = bytes.Length; //Count bytes to send
os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length); //Send it
}
catch (WebException ex)
{
MessageBox.Show(ex.Message, "HttpPost: Request error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (os != null)
{
os.Close();
}
}
try
{ // get the response
WebResponse webResponse = webRequest.GetResponse();
if (webResponse == null)
{ return null; }
StreamReader sr = new StreamReader(webResponse.GetResponseStream());
return sr.ReadToEnd().Trim();
}
catch (WebException ex)
{
MessageBox.Show(ex.Message, "HttpPost: Response error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return null;
}
看看嗎? ASMX,WCF(RESTful或SOAP?),ASP.NET MVC Web-API等? – Dai 2012-08-06 19:25:41
SDO REST API ... – Testifier 2012-08-06 19:33:07
Amazon S3 ...... – Testifier 2012-08-06 19:34:08