将ByteArray发布到WebService,在android中返回错误的请求

时间:2015-10-31 11:37:30

标签: java android web-services

我有一个Web服务托管 - 当我通过C#中的客户端应用程序发送数据时,它能够成功发布并从服务器响应 - 但我的android应用程序返回400:错误请求

a)我确保网址是正确的 b)该应用程序具有适当的权限 c)我已经确保传递给函数的byte []是正确的 - 因为我颠倒了进程以检查它是否能够形成文件 - 它确实如此。

以下是C#中的代码 - (正在运行)

public static string PostAudio(byte[] bytes, string orderid, string mobilenumber, string fileformat, string status){
    try
    {
        string _serviceaudiourl = "https://www.capricorn.cash/getway.svc/uploadaudio/?token={0}&orderid={1}&mobilenumber={2}&fileformat={3}&status={4}"; 
        string _servicetoken = "DD9BBF0C749261F3A3F5D9AE62CFE29E"; 
        string serviceUrl = string.Format(_serviceaudiourl, _servicetoken, orderid, mobilenumber, fileformat, status);

        string response = "";

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(serviceUrl);
        req.Method = "POST";
        req.Timeout = 120000;
        req.ContentLength = bytes.Length;
        using (Stream requeststream = req.GetRequestStream())
        {
            requeststream.Write(bytes, 0, bytes.Length);
            requeststream.Close();
        }


        using (HttpWebResponse webResponse = (HttpWebResponse)req.GetResponse())
        {
            using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
            {
                response = sr.ReadToEnd().Trim();
                sr.Close();
            }
            webResponse.Close();
        }
        return response;
    }
    catch (Exception ex)
    {
        return "Error:" + ex.Message;
    }

这是我的Android功能

public String PostAudio(byte[] bytes1, String orderid, String mobilenumber, String fileformat, String status) 
    {
        String serviceUrl;
        try
        {
            String _servicetoken = "DD9BBF0C749261F3A3F5D9AE62CFE29E"; 
            serviceUrl =  "https://www.capricorn.cash/getway.svc/uploadaudio/?token=" + _servicetoken + "&orderid=" + orderid + "&mobilenumber=" + mobilenumber + "&fileformat=" + fileformat +  "&status=" + status; 
            java.net.URL iurl = new java.net.URL(serviceUrl);

            HttpURLConnection req = (HttpURLConnection) iurl.openConnection();
            req.setDoInput(true);
            req.setDoOutput(true);  
            req.setRequestMethod("POST");
            req.setConnectTimeout(120000);    
            req.setUseCaches (false);     
            req.setRequestProperty("Content-Length", String.valueOf(bytes1.length));

            DataOutputStream wr = new DataOutputStream (req.getOutputStream());                            
            wr.write(bytes1,0, bytes1.length);
            wr.flush ();
            wr.close ();

            InputStream is;
            int stat = req.getResponseCode();

            if (stat == 400){
                is = req.getErrorStream();
            }
            else{
                is = req.getInputStream();
            }

            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer(); 
            while((line = rd.readLine()) != null) {
              response.append(line);
              response.append('\r');
            }
            rd.close();
            return response.toString();
        }
        catch (IOException e) {
            return "IO Error:" + e.toString();
        }
        catch (Exception ex)
        {
            return "Error:" + ex.toString();
        }
    }

..可以告诉我到底哪里出错!?!

0 个答案:

没有答案