简体   繁体   English

从Android将图像发布到WCF Rest服务

[英]Posting image to wcf rest service from android

Im having a problem to post a image to my wcf rest service. 我在将图像发布到我的wcf rest服务时遇到问题。 Im posting some parameters wich one of them is a base64 utf-8 encoded string (the image). 我张贴了其中一个参数的一些参数是base64 utf-8编码的字符串(图像)。

My problem is that everytime i post i get "bad request". 我的问题是,每当我发布邮件时,我都会收到“错误的请求”。 Here is the code 这是代码

public String PostImage(Drawable img) throws Exception 
{
    HttpPost httpost = new HttpPost("http://10.0.2.2:1374/uploaditem");
    JSONStringer json = JSONStringer()
        .object()
        .key("ipm")
            .object()
                .key("name").value("test")
                .key("description").value("asfa")
                .key("categoryid").value(1)
                .key("data").value(ConvertImgToBase64Str(img))
                .key("imagetype").value(2)
                .key("tags").value("test;test")
            .endObject()
         .endObject();

    StringEntity entity = new StringEntity(json.toString());
    entity.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8                  
    entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
    httpost.setEntity(entity); 
    return ExcecutePostRequest(httpclient,httpost);
}

//Method to convert the image to base64encoded string
private String ConvertImgToBase64Str(Drawable img) {
    Bitmap bitmap = ((BitmapDrawable)img).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] bitmapdata = stream.toByteArray();
    return Base64.encodeToString(bitmapdata, Base64.URL_SAFE);
}

It is something with the encoded string, but what? 它与编码的字符串有关,但是呢?

I don't see why it should be a problem with the encoded string. 我不明白为什么编码字符串应该有问题。

Firstly, try removing the data attribute in the string object and see if you get the same problem. 首先,尝试删除字符串对象中的data属性,看看是否遇到相同的问题。 This way you can eliminate the possibility it is due to the the encoding and ensure you are making the request correctly. 这样,您就可以消除由于编码引起的可能性,并确保正确发出请求。

Secondly print the http message being sent and format check it. 其次,打印正在发送的http消息并进行格式检查。 If you have access to the server log the message being received and any details that may elaborate on the Bad Request . 如果您有权访问服务器日志,则将接收到消息,并在Bad Request上详细说明所有详细信息。 I would have thought WCF will be printing something to stderr if it is responding with a bad request so try just checking the existing logs first. 我本以为WCF如果响应错误的请求将向stderr打印某些内容,因此请尝试先检查现有日志。

EDIT 编辑

I don't think there should be problems with strange characters because the character used in base64 encoding don't fall outside the ASCi range. 我认为奇怪的字符不会出现问题,因为base64编码中使用的字符不会超出ASCi范围。

You should check the size of the request being sent. 您应该检查所发送请求的大小。 If you images are big you will get a big base64 encoded string which might exceed the server post limit. 如果图像较大,则将获得较大的base64编码字符串,该字符串可能超出服务器发布限制。

If you can't get to the server logs which I think would clarify this if it was the problem you could test it by sending a smaller image. 如果无法访问服务器日志,我认为这将澄清该问题,那么可以通过发送较小的图像来对其进行测试。

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

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