wcf - 无法将图像上传到WCF Rest服务

标签 wcf rest post image-upload

我正在创建WCF Rest服务以从移动应用程序上传图像。但我越来越
远程服务器返回错误:(400)错误的请求。谁能指出我做错了什么。
以下是我的定义:

    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/PostImage",Method ="POST")]

 PublicMessage PostImage(Upload obj);

[DataContract]
    public class Upload
    {
        [DataMember]
        public Stream File { get; set; }
    }


服务定义:

public PublicMessage PostImage(Upload obj)
    {
        byte[] buffer = StreamToByte(obj.File);   //Function to convert the stream to byte array
     FileStream fs = new FileStream(@"D:\ShopMonkeyApp\Desert.jpg", FileMode.Create, FileAccess.ReadWrite);
        BinaryWriter bw = new BinaryWriter(fs);

        bw.Write(buffer);

        bw.Close();

        return new PublicMessage { Message = "Recieved the image on server" };
    }


客户申请:

string filePath = @"D:\ShopMonkeyApp\Desert.jpg";

        string url = "http://localhost:50268/shopmonkey.svc/PostImage/"; // Service Hosted in IIS

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        request.Accept = "text/xml";

        request.Method = "POST";

        request.ContentType = "image/jpeg";

        using (Stream fileStream = File.OpenRead(filePath))

        using (Stream requestStream = request.GetRequestStream())
        {

            int bufferSize = 1024;

            byte[] buffer = new byte[bufferSize];

            int byteCount = 0;

            while ((byteCount = fileStream.Read(buffer, 0, bufferSize)) > 0)
            {

                requestStream.Write(buffer, 0, byteCount);

            }

        }

        string result;

        using (WebResponse response = request.GetResponse())

        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {

            result = reader.ReadToEnd();

        }

        Console.WriteLine(result);


网络配置:

 <system.serviceModel>
        <services>
      <service name="ShopMonkey.ShopMonkey" behaviorConfiguration="ServiceBehaviour">
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="webHttpBinding" contract="ShopMonkey.IShopMonkey" behaviorConfiguration="web">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.behaviorConfiguration="web"
          -->
        </endpoint>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
          <dataContractSerializer maxItemsInObjectGraph="10000000"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>


谢谢

维杰

最佳答案

在web.config中增加消息队列长度可以解决我的问题。

<webHttpBinding>
        <binding name="streamWebHttpbinding" transferMode="Streamed"  maxReceivedMessageSize="1000000000000" receiveTimeout="01:00:00" sendTimeout="01:00:00" />
      </webHttpBinding>


谢谢大家

关于wcf - 无法将图像上传到WCF Rest服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11507963/

相关文章:

asp.net - 提供的 URI 方案 'https' 无效;预期 'http' 。参数名称 : via

python - Django Tastypie 添加 Content-Length header

javascript - JQuery $.ajax(),$.post() 发送 GET 作为请求方法

c++ - 带有证书验证的 native C++ HTTPS REST 调用

wordpress - WP_Query 多个帖子类型和分类法

security - 允许获取请求但仅在我的域中?

java - Android 到 WCF 连接被对等方重置

wcf - WCF MaxReceivedMessageSize 有什么意义

c# - WCF 反序列化中的 XmlException : "Name cannot begin with ' <'" - in automatic property backing fields

java - 使用 Jersey 和 JAX-RS 进行 POST 上传时换行符丢失