简体   繁体   English

使用HttpClient将URL中的JSON查询字符串发送到Web服务(Java)

[英]Using HttpClient to send JSON query string in a URL to a web service (Java)

I have a web service that I built... what I am trying to do now is send a simple request that contains a json query string from a Tapestry web app to that web service. 我有一个构建的Web服务...我现在要做的是将一个简单请求(其中包含一个Tapestry Web应用程序的json查询字符串)发送到该Web服务。 I searched around and most people say to use Apache HttpClient to achieve this. 我到处搜索,大多数人说要使用Apache HttpClient来实现这一目标。 Along with HttpClient I am using URIBuilder. 与HttpClient一起使用URIBuilder。

The Json object that I am trying to send looks like this 我尝试发送的Json对象看起来像这样

{"user":{"userEmail":"jdoe@gmail.com","firstName":"John","lastName":"Doe","phone":"203- 555-5555"},"password":"dead"} {“ user”:{“ userEmail”:“ jdoe@gmail.com”,“ firstName”:“ John”,“ lastName”:“ Doe”,“ phone”:“ 203- 555-5555”},“ password” :“死”}

*I realize the issues with the password being sent in plain text etc... *我意识到以纯文本等方式发送密码的问题...

The url that works (tested by manually entering in a web browser and this web service already services an Android client and an iOS client) looks like this 可用的网址(通过手动输入网络浏览器进行测试,并且该网络服务已为Android客户端和iOS客户端提供服务)

http:// ##.##.###.##/createuser?json={"user":{"userEmail":"jdoe@gmail.com","firstName":"John","lastName":"Doe","phone":"203-555-5555"},"password":"dead"} http:// ##。##。###。## / createuser?json = {“ user”:{“ userEmail”:“ jdoe@gmail.com”,“ firstName”:“ John”,“ lastName” :“ Doe”,“ phone”:“ 203-555-5555”},“ password”:“ dead”}

Here is the HttpClient code that I have mashed together from google'ing around trying to figure out why this wont work. 这是我从谷歌那里拼凑在一起的HttpClient代码,试图弄清楚为什么它不起作用。 Essentially what I am trying to do is create a URI with URIBuilder and then construct an HttpPost or HttpGet object with the newly built URI. 本质上,我想做的是使用URIBuilder创建URI,然后使用新建的URI构造HttpPost或HttpGet对象。 But something is going wrong in the URIBuilding process. 但是在URIBuilding过程中出了点问题。 When I debug, an exception gets thrown when I try to set all the aspects of the URI. 调试时,尝试设置URI的所有方面时都会引发异常。

Object onSuccess() throws ClientProtocolException, IOException, URISyntaxException{
    // json = {"user":{"userEmail":"jdoe@gmail.com","firstName":"John","lastName":"Doe","phone":"203- 555-5555"},"password":"dead"}
    String json = user.toJson();
    URIBuilder builder = new URIBuilder();
    // Error gets thrown when I step over the next line
    builder.setScheme("http").setHost("##.###.##.###").setPort(8080).setPath("createuser").setQuery("json=" +json); 
    URI uri = builder.build();

    HttpPost request = new HttpPost(uri);
    DefaultHttpClient httpClient = new DefaultHttpClient();
    String tmp = request.getURI().toString();

    HttpResponse response = httpClient.execute(request); 
    index.setResponse(EntityUtils.toString(response.getEntity()));
    return index;

The error that comes back when I step over the line that I commented in the code is 当我越过代码中注释的行时返回的错误是

[ERROR] TapestryModule.RequestExceptionHandler Processing of request failed with uncaught exception:org.apache.http.client.utils.URLEncodedUtils.parse(Ljava/lang/String;Ljava/nio/charset/Charset;)Ljava/util/List; [错误] TapestryModule.RequestExceptionHandler处理请求失败,出现未捕获的异常:org.apache.http.client.utils.URLEncodedUtils.parse(Ljava / lang / String; Ljava / nio / charset / Charset;)Ljava / util / List; java.lang.NoSuchMethodError:org.apache.http.client.utils.URLEncodedUtils.parse(Ljava/lang/String;Ljava/nio/charset/Charset;)Ljava/util/List; java.lang.NoSuchMethodError:org.apache.http.client.utils.URLEncodedUtils.parse(Ljava / lang / String; Ljava / nio / charset / Charset;)Ljava / util / List;

I have tried a lot of other combinations of methods and objects to get this request to send off to the server correctly and nothing seems to work. 我已经尝试了许多其他方法和对象的组合,以使此请求正确发送到服务器,但似乎没有任何效果。 Hopefully I am overlooking something relatively simple. 希望我能忽略一些相对简单的事情。

Thanks in advance for any guidance you can provide. 在此先感谢您提供的任何指导。

You most likely have the wrong version or two versions of the apache httpcomponents on your classpath. 您很可能在类路径上使用了错误的一两个版本的apache httpcomponents。 If you are running Tapestry it will print out all packages on the classpath on the error page. 如果您正在运行Tapestry,它将在错误页面上的类路径上打印出所有软件包。 Investigate there, find which httpcomponents is loaded, figure out where it comes from and fix it. 在此处进行调查,找到要加载的httpcomponents,找出它的来源并进行修复。

If this does not work, you should share some of your runtime environment with us. 如果这不起作用,则应与我们共享一些运行时环境。 Which servlet engine, running from which IDE or are you running from the command line. 哪个servlet引擎,从哪个IDE运行,或者从命令行运行。 Are you using Maven? 您在使用Maven吗? If so share your pom. 如果是这样,请分享您的pom。 Etc. 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM