C # HttpWebRequest send multipart / form-data to POST

1

I need to send POST using HttpWebRequest , with header Content-Type=multipart/form-data and Accept=multipart/form-data , and data in Body .

My routine below, I use it without problems doing POST , sending data in the format Json .

Problem

I need to put in the current routine, parameters for multipart/form-data , and already tried in various ways, but none worked.

The data I have to send is 3 text fields.

Current Routine

protected virtual RetornoSolicitacao Executa(Operacao operacao, string urlBase, string urlServico, string dados)
{
    HttpWebRequest requisicao = MontaRequisicao(urlBase, urlServico);           

    byte[] dadosFormatados = Encoda(dados);
    requisicao.ContentLength = dadosFormatados.Length;

    Stream requisicaoDados = requisicao.GetRequestStream();
    requisicaoDados.Write(dadosFormatados, 0, dadosFormatados.Length);
    requisicaoDados.Close();

    HttpWebResponse resposta;

    resposta = (HttpWebResponse)requisicao.GetResponse();

    resultado.HttpCodigoRetorno = (int)resposta.StatusCode;
    var respostaTexto = new StreamReader(resposta.GetResponseStream());
    resultado.Retorno = respostaTexto.ReadToEnd();
}

Required

protected virtual HttpWebRequest MontaRequisicao(string urlBase, string urlServico)
{
    HttpWebRequest requisicao = default(HttpWebRequest);

    Uri url = new Uri(new Uri(urlBase), urlServico);
    requisicao = (HttpWebRequest)WebRequest.Create(url);

    requisicao.ContentType = "multipart/form-data";
    requisicao.Accept = "multipart/form-data";                                    
    requisicao.Method = "POST";
    requisicao.Timeout = ConfiguarTimeOut(_configuracao.Configuracao) * 1000;               
    requisicao.Headers.Add("Authorization", chaveAutorizacao);               

    return requisicao;          
}
    
asked by anonymous 26.07.2018 / 22:25

0 answers