Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

HttpClient PostAsync MultipartFormData returns 403

$
0
0

Hi,

First of all I'm sorry for my english.

I'm trying to make chat app with XF and I'm trying to upload a file with parameters to server. But I'm getting always 403 Forbidden message. (There is no authentication, there is only token key for now).

If I try to get or send any data to server, it works as well. When I try to send a file with data it returns 403 Forbidden message. I also tried to send same data with Postman. it's worked as well. I'm writing part of code, Could you please tell me, I made it wrong where?

Thanks in advance.

private async Task<HttpClient> GetClient() { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Accept", "application/json"); client.DefaultRequestHeaders.Add("X-Token-Key", ServiceToken); client.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("Chrome", "41.0.2228.0")); return client; }

If I send text message, it works as well.
public async Task<MobileResult> SendConversationTextMessage(MessageModel message) { HttpClient client = await GetClient(); string param = JsonConvert.SerializeObject(message); var response = await client.PostAsync(Url + "conversation/message_add_text", new StringContent(param, Encoding.UTF8, "application/json")); var mobileResult = JsonConvert.DeserializeObject<MobileResult>(await response.Content.ReadAsStringAsync()); return mobileResult; }

If I send message with data, it returns 403 Forbidden
`public async Task SendConversationFileMessage(
FileModel FileMessage,
int UserRemoteId,
int ConversationId,
int ToUserId,
string SendedTime,
MessageModel.MessageType Type,
MessageModel.MessageStatus Status,
string MessageType)
{

        HttpClient client = await GetClient();
        string PostUrl = Url + "conversation_message_add_" + MessageType;

        MultipartFormDataContent content = new MultipartFormDataContent();
        ByteArrayContent baContent = new ByteArrayContent(FileMessage.BinaryData);
        StringContent UserIdContent = new StringContent(UserRemoteId.ToString());
        StringContent ConversationIdContent = new StringContent(ConversationId.ToString());
        StringContent ToUserIdContent = new StringContent(ToUserId.ToString());
        StringContent SendedTimeContent = new StringContent(SendedTime.ToString());
        StringContent TypeContent = new StringContent(Type.ToString());
        StringContent StatusContent = new StringContent(Status.ToString());

        content.Add(baContent, "AttachedFile", FileMessage.Name);
        content.Add(UserIdContent, "UserId");
        content.Add(ConversationIdContent, "ConversationId");
        content.Add(ToUserIdContent, "ToUserId");
        content.Add(SendedTimeContent, "SendedTime");
        content.Add(TypeContent, "Type");
        content.Add(StatusContent, "Status");

        try
        {
            var response = await client.PostAsync(PostUrl, content);
            string result = await response.Content.ReadAsStringAsync();
            var mobileResult = JsonConvert.DeserializeObject<MobileResult>(result);
            return mobileResult;

        }
        catch (Exception e)
        {
            return new MobileResult
            {
                Result = false,
                Data = null,
                Message = e.ToString()
            };
        }
    }

`

You can see Postman SS from attachment.


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>