简体   繁体   English

对WCF REST(JSON)的POST请求-WebException(400)错误的请求

[英]POST request to WCF REST (json) - WebException (400) bad request

I wanna upload a file to db by web Service (REST - WCF) but I have WebException (400) bad request, I read many solution but my code still not working! 我想通过Web服务(REST-WCF)将文件上传到db,但是我有WebException(400)错误的请求,我阅读了许多解决方案,但是我的代码仍然无法正常工作!

.config .config

<system.serviceModel>
        <bindings>
            <webHttpBinding>
                <!--Limits to 10MB-->
                <binding name="ApiQuotaBinding"
                         maxReceivedMessageSize="1048576000"
                         maxBufferPoolSize="1048576000"
                         maxBufferSize="1048576000"
                         closeTimeout="00:03:00"
                         openTimeout="00:03:00"
                         receiveTimeout="00:03:00"
                         sendTimeout="00:03:00"
                         >
                    <readerQuotas maxDepth="32"
                                  maxStringContentLength="104857600"
                                  maxArrayLength="1048576000"
                                  maxBytesPerRead="1048576000"
                                />
                    <security mode="None" />
                </binding>
            </webHttpBinding>
        </bindings>
        <services>
            <service name="TransferService">
                <endpoint address=""
                          binding="webHttpBinding"
                          bindingConfiguration="ApiQuotaBinding"
                          contract="ITransferService"
                          behaviorConfiguration="webHttpBehavior"/>
                <endpoint address="mex"
                          contract="IMetadataExchange"
                          binding="mexHttpBinding"/>
            </service>
        </services>
        <behaviors>
            <endpointBehaviors>
                <behavior name="webHttpBehavior">
                    <webHttp/>
                </behavior>
            </endpointBehaviors>
            <serviceBehaviors>
                <behavior >
                     <!--To avoid disclosing metadata information, set the values below to false before deployment--> 
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
        </behaviors>

this is my web method: 这是我的网络方法:

public Guid UploadFile(byte[] ByteStream)
{
    Guid id = Guid.Empty;
    //upload
    using (RepoDbWave dbc = new RepoDbWave())
    {
        FileItem f = new FileItem();

        var count_row = dbc.FileItems.Count(a => a.ID != Guid.Empty);
        f.FileContent = ByteStream;
        f.FileSize = f.FileContent.Length;
        f.Time = DateTime.Now;
        FileItem newItem = dbc.FileItems.Add(f);
        dbc.SaveChanges();
        id = newItem.ID;
    }
    return id;
}

and my request code: 和我的请求代码:

private void btnUpload_Click(object sender, EventArgs e)
{

    OpenFileDialog open = new OpenFileDialog();
    open.Filter = "Wave files (*.*)|*.*";
    if (open.ShowDialog() == DialogResult.OK)
    {
        string WaveLocation = open.FileName;
        txtUpload.Text = WaveLocation;
        byte[] WavebyteArray = File.ReadAllBytes(WaveLocation);

        ///webClient//////////////////////////////////////////////
        WebClient Proxy1 = new WebClient();
        Proxy1.Headers["Content-type"] = "application/json";
        MemoryStream ms = new MemoryStream();
        DataContractJsonSerializer serializerToUplaod = new DataContractJsonSerializer(typeof(byte[]));
        serializerToUplaod.WriteObject(ms, WavebyteArray);
        byte[] data = Proxy1.UploadData("http://localhost:1866/TransferService.svc/UploadFile", "POST", ms.ToArray());
        MemoryStream stream = new MemoryStream(data);
        DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(byte[]));
        var guID = obj.ReadObject(stream);
        lblUpload.Text = guID.ToString();
        //////////////////////////////////////////////////////////
    }

I think the problem is you are setting the content-type as application/json. 我认为问题是您将内容类型设置为application / json。 But you are passing a byte array in the body. 但是,您正在体内传递字节数组。 This might confuse the WCF. 这可能会使WCF感到困惑。 Instead of using content-type as json try using any stream. 不要使用content-type作为json尝试使用任何流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM