FileNotFoundException而在Httpman上使用在Postman上工作的HttpURLConnection类在WebAPI上发布数据

时间:2018-06-28 07:01:20

标签: http-post httpurlconnection filenotfoundexception

我正在尝试将数据(编码的)发布到服务器上,但是它在读取响应代码为500的输入流(urlConnection.getInputStream())时抛出FileNotFoundException异常。此外,它仅在异常中使用不完整的URL返回BaseURL请求参数。我已经检查了所有以前的解决方案,但它们对我不起作用。请不要将其标记为重复。在我的情况下,如果发送BadgeData'1005',则我收到响应,但字符串D1抛出错误。但是在邮递员上的同样工作也成功了。

 try {


            String D1 ="491007|]ÃN\\u0018j\\u000fo?Ú\\u001a?x\\u0003*<]5ì«ÅP¡3\\u0014¦\\f\u007Få?ÕË\\u00165À\\u0005\\u0002åä''÷b\\u0003?á\\u001dæ\u009DeK?µº?ÃP?7Ã}xé+;lÊG°Q\\u0012ÃìÚÉ\u008DtaßÍ%?\\t|ÊpØ\\u0003\\u001ek°«\\u000b?¾\\u0010??uv\\u001càA?´ÝIù\\fBk\\u0011\\u0006{¢Â[º\\u0002ED+³Â[º\\u0002ED+³Â[º\\u0002ED+³Â[º\\u0002ED+³Â[º\\u0002ED+³Â[º\\u0002ED+³Â[º\\u0002ED+³Â[º\\u0002ED+³Â[º\\u0002ED+³Â[º\\u0002ED+³\\u0006\\u000b\\u0012u??\\u0007û";

            RequestObj req = new RequestObj();
            req.setBadgeData(D1);
            req.setDeviceName("A_iPod_02");

            URL url = new URL("http://localhost:49999/Spotlighter_Service/Micongress/GetDetails" );
            Log.e("url value is ",URLEncoder.encode(devicename.getText().toString(), "UTF-8"));
             HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
             urlConnection.setDoOutput(true);
             urlConnection.setDoInput(true);
             urlConnection.setRequestMethod("POST");

            DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
            out.writeBytes(req.toString());
            Log.e("MainActivity","response is "+urlConnection.getResponseCode());
            out.flush();
            out.close();
            InputStream content = urlConnection.getInputStream();

            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content));
                StringBuilder stringBuilder = new StringBuilder();
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line).append("\n");
                }
                bufferedReader.close();

                return stringBuilder.toString();
            }
            finally{
                urlConnection.disconnect();
            }
        }
        catch(Exception e) {
            Log.e("ERROR", e.getMessage(), e);
            return e.getMessage();
        }
    }

0 个答案:

没有答案