c# - 从 Android 调用到 C# 的服务不起作用

标签 c# android web-services post web

我想做的是将我的图像从 Android 发送到我的 C# Web 服务。 在 Android 方面,我没有收到任何错误和警告,但在服务上没有任何可显示的内容,它根本没有真正获取图像。

我对这种服务仍然特别陌生,所以非常感谢任何帮助!!

我的 Android AsyncTask 看起来像这样:

@Override
        protected String doInBackground(File... file) {

            String imageDescriptionTemp = "Photo Temp Description.";
            String PostRequestUri = "https://demo.relocationmw.com/ws_docmgmt/Service1.svc";
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(PostRequestUri);
            FileBody bin1 = new FileBody(file[0]);
            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            entity.addPart("Image", bin1);
            post.setEntity(entity);
            HttpResponse response;
            try {
                response = client.execute(post);
                resEntity = response.getEntity();
                final String response_string = EntityUtils.toString(resEntity);
                if(resEntity != null){
                Log.i("RESPONSE", response_string); 
                }
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

只需调用服务器上的服务文件、发送图像并很快发送描述和所有内容。

这是我的 C# 服务代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Xml;


// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1
{
    #region vars
    XmlDocument doc = new XmlDocument();// Create the XML Declaration, and append it to XML document
    XmlElement root;

    #endregion

    public Stream GetImage(string path)
    {
        FileStream stream = File.OpenRead(@System.Web.Hosting.HostingEnvironment.MapPath("~/attachments/test/") + "SrvTest.jpg");
        WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
        return stream as Stream;
    }

    public XmlDocument UpImage(string path, Stream filecontents)
    {

        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);

        doc.PreserveWhitespace = true;

        doc.AppendChild(dec);// Create the root element

        root = doc.CreateElement("UpImage");

        doc.AppendChild(root);


        XmlElement xml_item;
        xml_item = doc.CreateElement("Response");

        Image img = System.Drawing.Image.FromStream(filecontents);
        img.Save(System.Web.Hosting.HostingEnvironment.MapPath("~/attachments/test/") + "SrvTest.jpg", ImageFormat.Jpeg);

        XmlElement item;
        item = doc.CreateElement("Success");
        item.InnerText = "Success";
        xml_item.AppendChild(item);

        return doc;

    }
}

这应该接受我的文件并返回一个带有 SUCCESS 的 XML 供我记录。 再次感谢您的帮助!

最佳答案

假设您的应用是加载到 IE 中的普通 html 表单,并准确发送该表单将发送的内容。这样您就可以使用简单的测试表单测试您的服务器组件。

首先,编写一个向您的服务提交图像的纯 html 表单。使用类型属性设置为"file"的输入元素,并确保给元素一个名称,而不仅仅是一个 id...

一旦您可以确认 html 表单成功发布,您就会知道问题出在您的 android 客户端代码中。

如果问题出在您的 android 代码中,请检查正文的内容。查看它是否不为空等,然后检查是否正确设置了 Content-Type 和 Content-Disposition header 。

如果以上所有测试都通过,请检查您帖子的边界线。几年前,我两次差点因为这个问题而出轨。 (不可见的字符计数。)

这是一个很好的例子:Uploading image to server in Multipart along with JSON data in Android

虽然它有不同的后端(Java),但 http 规则是相同的。

-布兰登

关于c# - 从 Android 调用到 C# 的服务不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15977217/

相关文章:

web-services - WCF服务不遵守超时配置

c# - 什么时候是撇号而不是撇号 - 验证.Net/Javascript

c# - 是否可以为 path.Data 设置动画

android - 如何在Android Studio中解析 "multiple dex files define Lorg/junit/runner/Runner"

android - 无法安装第三方APK文件

android - 当我将图像放入可绘制文件夹时,为什么 BitmapFactory.decodeResource() 会变慢?

c# - 提取键值对

c# - 如何将 Objective-C 静态库绑定(bind)到 Xamarin.iOS?

c# - WCF 不反序列化。为什么我的参数是 NULL?

java - 如何在java中的webservices中返回json作为响应