zz 试图将 JSON 发送到.NET web 服务使用 HttpURLConnection FileNotFoundException

http://www.itstrike.cn/Question/29e46085-7fd4-4deb-afd7-4572ed5d591a.html

https://stackoverflow.com/questions/20987525/filenotfoundexception-trying-to-send-json-to-net-webservice-using-httpurlconnec

I am trying to parse a response from an InputStream from a HttpURLConnection using the following code.I get an error when trying to get the InputStream.I am just looking to get a response to check if the web-servcice call worked OK.

     static final String SOAP_ACTION="http://888topup.com/ImageProcess.svc/UploadImage";
    java.net.URL url=null;
    try {
        url = new java.net.URL(SOAP_ACTION);
    } catch (MalformedURLException e) {
        Log.e(TAG, "Bad URL",e);
    }
    JSONObject obj=new JSONObject();
    try {
        obj.put("Vaue", value);
    } catch (JSONException e) {
        Log.e(TAG, "Create JSONObjerct throws an error");
    }
    HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection)url.openConnection();
    } catch (IOException e1) {
        Log.e(TAG,"Error opening connection",e1);
    }
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);
    try {
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Accept", "*/*");
        urlConnection.addRequestProperty("Accept-Encoding", "gzip deflate sdch");
        urlConnection.setRequestProperty("Content-Type","application/json");
    } catch (ProtocolException e) {
        Log.e(TAG,"Error setting header",e);
    }
    try {
        OutputStream os=urlConnection.getOutputStream();
        BufferedOutputStream bos=new BufferedOutputStream(os);
        byte[] data=obj.toString().getBytes();
        bos.write(data);
        InputStream is=urlConnection.getInputStream();
        BufferedReader br=new BufferedReader(new InputStreamReader(is));
        String response;
        response=br.readLine();

        Log.d(TAG, "Response: "+response);
        bos.close();
        br.close();
        is.close();
        urlConnection.disconnect();
    } catch (IOException e) {
        Log.e(TAG,"Error peforming read-write operations",e);
    }

Here is the Logcat entry for what happened when I tried to run this code:

  MainActivity(9628): Error peforming read-write operations
  MainActivity(9628): java.io.FileNotFoundException: http://888topup.com/ImageProcess.svc/UploadImage
  MainActivity(9628):   at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
  MainActivity(8989):   at com.example.imageuploadtest.MainActivity.sendStringToServiceUsingRest(MainActivity.java:184)
  MainActivity(8989):   at com.example.imageuploadtest.MainActivity.access$0(MainActivity.java:149)
  MainActivity(8989):   at com.example.imageuploadtest.MainActivity$1$1.run(MainActivity.java:66)

EDIT:

The operation as specified in WSDL:

<wsdl:operation name="UploadImage">
<wsdl:input wsaw:Action="http://tempuri.org/IImageProcess/UploadImage"   message="tns:IImageProcess_UploadImage_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IImageProcess/UploadImageResponse" message="tns:IImageProcess_UploadImage_OutputMessage"/>
</wsdl:operation>

Adding urlConnection.connect() before writing data to the OutputStream does not work either.

EDIT:Using the URL(SOAP_ACTION) and passing jsondata works from Sencha-Touch for someone else who tested it.

Tried passing JSONObject as String instead of byte data.Did not work either?

java android inputstream httpurlconnection


shareimprove this question

edited Jan 8 ‘14 at 6:58

asked Jan 8 ‘14 at 5:14

vamsiampolu

1,87873692

 

1  

You‘ve omitted the actual exception. – EJP Jan 8 ‘14 at 5:16
    

@EJP added the exception and the URL – vamsiampolu Jan 8 ‘14 at 5:26
    

Now that you‘ve provided it, what part of not found ... http://888topup.com/ImageProcess.svc/UploadImage don‘t you understand? – EJP Jan 8 ‘14 at 5:27 
    

I tried passing JSON data to output stream to the service,does it mean that the request was not successfull,how do I get the response from the service without using InputStream – vamsiampolu Jan 8 ‘14 at 5:30
    

You‘re missing the point. It‘s the URL itself that‘s invalid. Nothing to do with the code. – EJP Jan 8 ‘14 at 5:31

show 2 more comments

1 Answer

activeoldestvotes


up vote0down voteaccepted

There were a few mistakes with what I was doing,firstly I was passing the wrong parameter Vaueinstead of Value to the service.

   JSONObject obj=new JSONObject();
    try {
        String value_key="Value";
        obj.put(value_key, value);
        Log.d(TAG,obj.toString());
    } catch (JSONException e) {
        Log.e(TAG, "Create JSONObjerct throws an error");
    }

Secondly,I was trying to parse a response when no response was being sent.So, I replace the code to get the InputStream with the following code:

 int response=urlConnection.getResponseCode();

The entire code is here:

java.net.URL url=null;
    try {
        url = new java.net.URL(SOAP_ACTION);
    } catch (MalformedURLException e) {
        Log.e(TAG, "Bad URL",e);
    }
    JSONObject obj=new JSONObject();
    try {
        String value_key="Value";
        obj.put(value_key, value);
        Log.d(TAG,obj.toString());
    } catch (JSONException e) {
        Log.e(TAG, "Create JSONObjerct throws an error");
    }
    HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection)url.openConnection();
    } catch (IOException e1) {
        Log.e(TAG,"Error opening connection",e1);
    }
    urlConnection.setDoOutput(true);
    try {
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Accept", "*/*");
        //urlConnection.addRequestProperty("Accept-Encoding", "gzip deflate sdch");
        urlConnection.setRequestProperty("Content-Type","application/json");
    } catch (ProtocolException e) {
        Log.e(TAG,"Error setting header",e);
    }
    try {
        urlConnection.connect();
        DataOutputStream dos=new DataOutputStream(urlConnection.getOutputStream());
        dos.write(obj.toString().getBytes());
        int response=urlConnection.getResponseCode();
        Log.d(TAG, "Response code: "+response);
        urlConnection.disconnect();
    } catch (IOException e) {
        Log.e(TAG,"Error peforming read-write operations",e);
    }

And finally I used this to send the data to the web-service as binary data:

  DataOutputStream dos=new DataOutputStream(urlConnection.getOutputStream());
    dos.write(obj.toString().getBytes());

shareimprove this answer

answered Jan 8 ‘14 at 7:47

vamsiampolu

1,87873692

 

    

You could avoid many problems when not using HttpURLConnection directly, but use a tiny wrapper likeDavidWebbHere is a Gist where you can see an example. – hgoebl Jan 8 ‘14 at 7:52

我想解析从响应 InputStream 从 HttpURLConnection 使用以下代码。试图获取时出现错误 InputStream 。我只想要得到的响应来检查是否 web servcice 调用工作 OK 。

     static final String SOAP_ACTION="http://888topup.com/ImageProcess.svc/UploadImage";     java.net.URL url=null;    try {        url = new java.net.URL(SOAP_ACTION);    } catch (MalformedURLException e) {        Log.e(TAG, "Bad URL",e);    }    JSONObject obj=new JSONObject();    try {        obj.put("Vaue", value);    } catch (JSONException e) {        Log.e(TAG, "Create JSONObjerct throws an error");    }    HttpURLConnection urlConnection = null;    try {        urlConnection = (HttpURLConnection)url.openConnection();    } catch (IOException e1) {        Log.e(TAG,"Error opening connection",e1);    }    urlConnection.setDoOutput(true);    urlConnection.setDoInput(true);    try {        urlConnection.setRequestMethod("POST");        urlConnection.setRequestProperty("Accept", "*/*");        urlConnection.addRequestProperty("Accept-Encoding", "gzip deflate sdch");        urlConnection.setRequestProperty("Content-Type","application/json");    } catch (ProtocolException e) {        Log.e(TAG,"Error setting header",e);    }    try {        OutputStream os=urlConnection.getOutputStream();        BufferedOutputStream bos=new BufferedOutputStream(os);        byte[] data=obj.toString().getBytes();        bos.write(data);        InputStream is=urlConnection.getInputStream();        BufferedReader br=new BufferedReader(new InputStreamReader(is));        String response;        response=br.readLine();

        Log.d(TAG, "Response: "+response);        bos.close();        br.close();        is.close();        urlConnection.disconnect();    } catch (IOException e) {        Log.e(TAG,"Error peforming read-write operations",e);    }

这里是 Logcat 条目,当我试图运行此代码时,发生了什么事:

  MainActivity(9628): Error peforming read-write operations  MainActivity(9628): java.io.FileNotFoundException: http://888topup.com/ImageProcess.svc/UploadImage  MainActivity(9628):   at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)  MainActivity(8989):   at com.example.imageuploadtest.MainActivity.sendStringToServiceUsingRest(MainActivity.java:184)  MainActivity(8989):   at com.example.imageuploadtest.MainActivity.access$0(MainActivity.java:149)  MainActivity(8989):   at com.example.imageuploadtest.MainActivity$1$1.run(MainActivity.java:66)

编辑:

WSDL中指定操作:

<wsdl:operation name="UploadImage"><wsdl:input wsaw:Action="http://tempuri.org/IImageProcess/UploadImage"   message="tns:IImageProcess_UploadImage_InputMessage"/><wsdl:output wsaw:Action="http://tempuri.org/IImageProcess/UploadImageResponse" message="tns:IImageProcess_UploadImage_OutputMessage"/></wsdl:operation>

添加 urlConnection.connect() 之前写入数据到 OutputStream 不会也不起作用。

编辑: 使用 URL(SOAP_ACTION) 和 jsondata 工程从传递煎茶触摸别人的人对其进行测试。

尝试将 JSONObject 作为字符串传递而不字节的数据。做了也不起作用吗?

解决方法 1:

有几个错误是在做什么,首先我传递错误的参数 Vaue 而不是 Value 的服务。

   JSONObject obj=new JSONObject();    try {        String value_key="Value";        obj.put(value_key, value);        Log.d(TAG,obj.toString());    } catch (JSONException e) {        Log.e(TAG, "Create JSONObjerct throws an error");    }

第二,我想解析响应时不正在发送响应。所以,我替换代码来获取 InputStream 用下面的代码:

 int response=urlConnection.getResponseCode();

这里是整个代码:

java.net.URL url=null;    try {        url = new java.net.URL(SOAP_ACTION);    } catch (MalformedURLException e) {        Log.e(TAG, "Bad URL",e);    }    JSONObject obj=new JSONObject();    try {        String value_key="Value";        obj.put(value_key, value);        Log.d(TAG,obj.toString());    } catch (JSONException e) {        Log.e(TAG, "Create JSONObjerct throws an error");    }    HttpURLConnection urlConnection = null;    try {        urlConnection = (HttpURLConnection)url.openConnection();    } catch (IOException e1) {        Log.e(TAG,"Error opening connection",e1);    }    urlConnection.setDoOutput(true);    try {        urlConnection.setRequestMethod("POST");        urlConnection.setRequestProperty("Accept", "*/*");        //urlConnection.addRequestProperty("Accept-Encoding", "gzip deflate sdch");        urlConnection.setRequestProperty("Content-Type","application/json");    } catch (ProtocolException e) {        Log.e(TAG,"Error setting header",e);    }    try {        urlConnection.connect();        DataOutputStream dos=new DataOutputStream(urlConnection.getOutputStream());        dos.write(obj.toString().getBytes());        int response=urlConnection.getResponseCode();        Log.d(TAG, "Response code: "+response);        urlConnection.disconnect();    } catch (IOException e) {        Log.e(TAG,"Error peforming read-write operations",e);    } 

我终于用这个来将数据发送到作为二进制数据的 web 服务:

  DataOutputStream dos=new DataOutputStream(urlConnection.getOutputStream());    dos.write(obj.toString().getBytes());
时间: 2024-09-27 08:50:33

zz 试图将 JSON 发送到.NET web 服务使用 HttpURLConnection FileNotFoundException的相关文章

用HttpPost 和 HttpClient 发送请求到web 端回调数据

btnok.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 跳转到新的页面 String name=usernameTextId.getText().toString(); String pwd=passwordTextId.getText().toString(); String url = "http://112.124.12.46/wxtest/login.soap?

基于Web Service的客户端框架搭建一:C#使用Http Post方式传递Json数据字符串调用Web Service

引言 前段时间一直在做一个ERP系统,随着系统功能的完善,客户端(CS模式)变得越来越臃肿.现在想将业务逻辑层以下部分和界面层分离,使用Web Service来做.由于C#中通过直接添加引用的方来调用Web Service的方式不够灵活,故采取手动发送Http请求的方式来调用Web Service.最后选择使用Post方式来调用Web Service,至于安全性和效率暂不考虑.在学习使用的过程,遇到了很多问题,也花了很长时间来解决,网上相关的帖子很少,如果各位在使用的过程中有一些问题难以解决,可

利用python httplib模块 发送Post请求测试web服务是否正常起来!

最近在学习python,恰好老大最近让我搞个基于post请求测试web服务是否正常启用的小监控,上网查了下资料,发现强大的Python恰好能够用上,所以自己现学现卖,顺便锻炼下自己. 由于本人也刚接触这块不久属于菜鸟级别,所以在任务之前,只能上网把基于post请求的web监控了解清楚,这些资料网上很多,因为本人认为完成这类任务最重要的就是要将实现的原理研究清楚,写程序只是实现的工具,如果大的逻辑不正确,后面的都是白忙活. 了解post发送请求的原理后,利用Python的httplib模块进行逻辑

Json发送数据中,使用的Java转义字符 KanKan原创

kankan原创 与php后台发送数据的时候,要求用到这种格式. private void sendJson(){ //初始化自定义的handler CashHandler handler = new CashHandler(this); //请求主地址,写在自定义Application中了,后面是接口名字 String url = MApplication.get().getAppServiceUrl() + "order/submit"; //自定义的发送请求方法 LReqEnti

使用SpringMVC+mybatis+事务控制+JSON 配置最简单WEB

最近在总结一些项目的基础知识,根据公司最近的一些意向和技术路线,初步整理了一个简单的配置例子     1.使用springmvc代替strutsMVC     2.使用请求json数据串的方式代替传统返回jspview.     3.使用Mybatis代替hibernate 在这些要求的基础上,做了一些尝试.    现在将配置文件整理如下:   目录结构如下:    1.web.xml <?xml version="1.0" encoding="UTF-8"?

[WebMethod]的使用,ajax调用[WebMethod]的使用,webservice(web服务) asmx的使用,ajax调用[WebMethod]进行json传输

首先,要分为.net 2.0和.net 3.5这两种情况来考虑 一:.net 2.0情况下 ajax可以调用 aspx.cs 里面加了 [webmethod]的静态方法,而不能调用 asmx 里面加了[webmethod]的方法或者是静态方法 调用aspx.cs里面的webmethod方法的时候,还必须在web.config里面添加如下代码 <httpModules> <add name="ScriptModule" type="System.Web.Han

Web服务之Nginx浅析

一.Nginx 简介: nginx [engine x]是Igor Sysoev编写的一个高性能的HTTP和反向代理服务器,另外它也可以作为邮件代理服务器. 在大多数情况下都是用来做静态web服务器和反向代理服务器,在作为反向代理服务器的时候,Nginx可以对后端的real server做负载均衡,基于应用层的负载均衡,但是他仅支持一些常见的协议,如:http.mysql.ftp.smtp. 特性: Nginx是一款面向性能设计的HTTP服务器,相较于Apache.lighttpd具有占有内存少

利用OpenShift托管Node.js Web服务进行微信公众号开发

最近写了一个微信的翻译机器人.用户只要关注该微信号,发送英文的消息,就能收到中文翻译的回复.后台是用Node.js写的,托管在OpenShift的Paas平台上.翻译过程实际上是调用微软的Bing translation API做的,代码中用到了alexu84的bing-translate和JacksonTian的wechat这两个npm模块.下面把做的过程详细说一下. 1. 微信公众号开发 首先是要到https://mp.weixin.qq.com 申请一个公众号,并申请成为开发者.目前个人只

转---高并发Web服务的演变——节约系统内存和CPU

[问底]徐汉彬:高并发Web服务的演变——节约系统内存和CPU 发表于22小时前| 4223次阅读| 来源CSDN| 22 条评论| 作者徐汉彬 问底Web服务内存CPU并发徐汉彬 摘要:现在的Web系统面对的并发连接数在近几年呈现指数增长,高并发成为了一种常态,给Web系统带来不小的挑战.一味地通过增加机器来解决并发量的增长,成本是非常高昂的.结合技术优化方案,才是更有效的解决方法. [导读] 徐汉彬曾在阿里巴巴和腾讯从事4年多的技术研发工作,负责过日请求量过亿的Web系统升级与重构,目前在小