使用http请求将图像发送到WCF服务

时间:2014-04-02 09:24:23

标签: c# wcf httpwebrequest console-application httprequest

我正在尝试使用来自简单控制台应用程序的http请求向wcf服务发送字节数组

这是我的代码:

*的接口:

[OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "uploadImage")]
    void UploadImage(byte[] recImg);
  • wcf service

    public void UploadImage(byte[] recImg)
    {
        try
        {
            if (recImg != null && recImg.Length != 0)
            {
                MemoryStream memStream = new MemoryStream(recImg, 0, recImg.Length);
                Image img = Image.FromStream(memStream);
                img.Save(@"img.jpg");
            }
        }
        catch (Exception ex)
        {
            Debug.Assert(false, ex.Message);
        }
    }
    

这是控制台应用程序代码:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("`    http://localhost:3765/ImageService.svc/uploadImage `");
            webRequest.Credentials = CredentialCache.DefaultCredentials;
            webRequest.Method = "POST";
            webRequest.ContentType = "application/octet-stream";
            webRequest.ContentLength = imgBytes.Length;



            Stream dataStream = webRequest.GetRequestStream();
            dataStream.Write(imgBytes, 0, imgBytes.Length);
            dataStream.Close();

            WebResponse response = webRequest.GetResponse();
            dataStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            reader.Close();
            response.Close();

请帮助我,我不知道这段代码有什么问题...... 它差不多3天了,我不知道如何解决这个问题!!

0 个答案:

没有答案