从Android上传文件到Azure中的WCF
本文关键字:Azure 中的 WCF 文件 Android | 更新日期: 2023-09-27 17:53:59
我正试图从android上传文件到azure,它快把我逼疯了。我认为这是设置中的问题,因为我已经确认了在IIS中与android一起工作的代码。现在我能够在c#中访问该方法,但我的流在服务器上是空的。在安卓系统中,我得到一个415错误"类型不匹配"。任何帮助都非常感谢!
这是我的c#测试客户端: ServiceReference1.PostImage2Request r = new ServiceReference1.PostImage2Request();
MemoryStream memStream = new MemoryStream();
using (FileStream fileStream = File.OpenRead("C:''crap.png"))
{
memStream.SetLength(fileStream.Length);
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
}
r.image = memStream;
ServiceReference1.RestServiceImplClient c = new ServiceReference1.RestServiceImplClient();
ServiceReference1.PostImage2Response p = c.PostImage2(r);
这是我的客户端配置:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IRestServiceImpl" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://testapp.cloudapp.net/Service1.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IRestServiceImpl"
contract="ServiceReference1.IRestServiceImpl" name="BasicHttpBinding_IRestServiceImpl" />
</client>
这是我的android代码:
String url = "https://testapp.cloudapp.net/Service1.svc/postimage2";
String filepath = String.format(Environment.getExternalStorageDirectory() + "/ImageStorage/1.jpg");
File f = new File(filepath);
if (f.exists())
{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
//This is the new shit to deal with MIME
MultipartEntity entity = new MultipartEntity();
entity.addPart("image", new FileBody(f, "multipart/form-data"));
httppost.setEntity(entity);
try {
String responseString = httpclient.execute(httppost, responseHandler);
我的服务器代码:
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "PostImage2")]
string PostImage2(Stream image);
public string PostImage2(Stream image)
{
MultipartParser parser = new MultipartParser(image);
string content = "testing123";
try
{
if (image == null)
return "Image stream is null";
// Read the stream into a byte array
byte[] data = parser.ToByteArray(image);
// Copy to a string for header parsing
content = Encoding.UTF8.GetString(data);
if (content == "")
content = "There is no content";
return content + " - dataLength: " + data.Length.ToString();
}
catch (Exception ex)
{
return "ERROR" + ex.Message + ex.InnerException + ex.StackTrace;
}
}
这是我的服务器配置:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="WCFServiceWebRole2.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_WCFServiceWebRole2" transferMode="Streamed" maxBufferSize="10485760" maxReceivedMessageSize="67108864">
<readerQuotas maxDepth="64" maxStringContentLength="214748364" maxArrayLength="214748364"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="WCFServiceWebRole2.Service1" behaviorConfiguration="WCFServiceWebRole2.Service1Behavior">
<endpoint address="" binding="basicHttpBinding" contract="WCFServiceWebRole2.IRestServiceImpl"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
你使用多部分格式有什么特别的原因吗?通常这样做是因为你想在一个请求中发送多个东西。例如,一个文件和一些相关的数据,或者几个文件,等等。你似乎没有这样做,所以多部分格式只是混淆的问题。除非您有很好的理由使用多部分格式,否则我将完全放弃它。让您的客户端发送原始字节,并让服务器代码直接从流中读取,而不使用MultipartParser
。
另外,我注意到,一旦你得到图像数据,你将尝试读取它作为UTF8数据。我很确定这行不通,很可能不是