【问题标题】:WCF Post Operation Contract returning 400 bad request For Android requestWCF Post Operation Contract 返回 400 bad request For Android request
【发布时间】:2015-09-05 00:26:41
【问题描述】:

我有一个 WCF 服务,当我尝试通过邮寄方式发送 json 并且我的操作合同有一个参数时,我收到 400 错误请求。

Obs:如果我的操作合同中没有这样的参数。相同的请求完美地工作。

[OperationContract] //无参数工作正常。 bool AddCadastroJSON();

[操作合同] bool AddCadastroJSON(String json); //带参数 400 错误请求。

我的合同

namespace CadastroTelefonesService
{
    [ServiceContract]
    public interface ICadastro
    {
        
        [WebInvoke(Method = "GET", ResponseFormat =WebMessageFormat.Json,UriTemplate = "addcliente/{nome};{telefone}")]
        [OperationContract]
        bool AddCadastro(String nome,  String telefone);

        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "listacliente")]
        [OperationContract]
        List<Cadastro> Lista();

        [WebInvoke(Method = "POST",ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json,UriTemplate = "addclientejson" )]
        [OperationContract]
        bool AddCadastroJSon(String cadastro);
    }
}

我在 Android 中的 HttpManager

  

/**
 * Created by Gabriel Santana on 8/26/2015.
 * Classe Responsável por fazer requisições HTTP e retornar um Request Package com o content.
 *
 */
public class HttpManager {
    public static String getData(RequestPackage p) {

        BufferedReader reader = null;
        HttpURLConnection con=null;
        //OkHttpClient client=null;
        URL url;
        String uri = p.getUri();
        if (p.getMethod().equals("GET")) {
            uri += "?" + p.getEncodedParams();
        }

        try {
            url = new URL(uri);
            con = (HttpURLConnection) url.openConnection();
            //client = new OkHttpClient();
            // con =  client.open(url);
            con.setRequestMethod(p.getMethod());
            JSONObject json = new JSONObject(p.getParams());
            String params = "params="+json.toString();
            if (p.getMethod().equals("POST")) {
                con.setDoOutput(true);
                con.setRequestProperty("Accept" , "application/json");
                con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                //OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
                OutputStream os = con.getOutputStream();
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, "UTF-8"));
                writer.write(params);
                writer.flush();

                writer.close();
                os.close();
            }

            StringBuilder sb = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            Log.i("fudeu",con.getResponseMessage()+con.getResponseCode());
            return sb.toString();

        } catch (Exception e) {
            try {
                Log.i("fudeu",con.getResponseMessage()+con.getResponseCode());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            return null;
        } finally {
            if (reader != null) {
                try {

                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }

    }

}

我已经尝试了 3 天,但我无法解决这个问题。

【问题讨论】:

    标签: java c# android web-services wcf


    【解决方案1】:

    我解决了这个问题配置客户端端点与主端点的配置相同。

    更改端点配置以接收 Wraped,

    更改我的数据合同以接收 Stream 并在之后反序列化。

        public bool AddCadastroJSon(Stream stream)
        {
           //Esse metodo recebe um stream pois a configuração o wcf não realiza o bind para a classe desejada.
            StreamReader reader = new StreamReader(stream);
            String JSONdata = reader.ReadToEnd();
            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            Cadastro cadastro = jsonSerializer.Deserialize<Cadastro>(JSONdata);
            var cadastroDao = new CadastroDao();
            return cadastroDao.AddCadastro(cadastro);
       }
    

    如果您将接收更改为流式传输,则需要删除 android 请求中的标头。 公共静态字符串 getData(RequestPackage p) {

        BufferedReader reader = null;
        HttpURLConnection con=null;
        //OkHttpClient client=null;
        URL url;
        String uri = p.getUri();
        if (p.getMethod().equals("GET")) {
            uri += "?" + p.getEncodedParams();
        }
    
        try {
            url = new URL(uri);
            con = (HttpURLConnection) url.openConnection();
            //client = new OkHttpClient();
            // con =  client.open(url);
            con.setRequestMethod(p.getMethod());
            JSONObject json = new JSONObject(p.getParams());
            String params = "{\"cadastro\":["+json.toString()+"]}";
            //String params =json.toString();
            if (p.getMethod().equals("POST")) {
                con.setDoOutput(true);
                //con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                //con.setRequestProperty("Content-Type", "application/json");
                //con.setRequestProperty("Accept" , "application/json");
                //OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
                OutputStream os = con.getOutputStream();
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, "UTF-8"));
                writer.write(p.getGson());
                writer.flush();
    
                writer.close();
                os.close();
            }
    
            StringBuilder sb = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
    
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            Log.i("fudeu",con.getResponseMessage()+con.getResponseCode());
            return sb.toString();
    
        } catch (Exception e) {
            try {
                Log.i("fudeu",con.getResponseMessage()+con.getResponseCode());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            return null;
        } finally {
            if (reader != null) {
                try {
    
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 2020-11-01
      • 1970-01-01
      • 2020-03-19
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      相关资源
      最近更新 更多