簡體   English   中英

WCF操作后合同返回400錯誤請求對於Android請求

[英]WCF Post Operation Contract returning 400 bad request For Android request

我有WCF服務,當我嘗試通過郵寄發送json並且我的操作合同有一個參數時,我收到400錯誤的請求。

觀察:如果我的操作合同中沒有這樣的參數。 相同的請求可以完美地工作。

[OperationContract] //沒有參數就可以了。 bool AddCadastroJSon();

[OperationContract] 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天,但我無法解決此問題。

我解決了使用與主端點相同的配置來配置客戶端端點的問題。

更改端點配置以接收Wraped,

更改“我的數據合同”以接收流並在之后反序列化。

    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;
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM