Django-Piston 和 Python 客户端与 Java 客户端
我使用 Django-Piston 构建了一个 Web 服务,允许 POST 和 GET 请求。作为测试的一部分,我编写了一个快速的 Python 脚本。使用该脚本我可以成功执行两种类型的请求;但是,当用 Java 编写的客户端尝试执行 POST 时,我收到错误: "POST /api/service/ HTTP/1.1" 400 225 "-" "Apache-HttpClient/4.1 (java 1.5)"
我的理解是 http任何语言生成的请求消息都必须相同。换句话说,如果我使用 python 客户端测试我的 Web 服务并且它可以工作,那么它应该适用于具有 http 库的所有其他语言。
这是 POST 的 python 代码:
import urllib, urllib2
data = urllib.urlencode({'url': 'www.uvic.ca', 'name': 'uvic'})
url = 'http://xxx/api/service/'
req = urllib2.Request(url, data)
print urllib2.urlopen(req).read()
这是 Java 代码:
HttpPost httpost = new HttpPost("http://xxx/api/service/");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("name", "some_name"));
nvps.add(new BasicNameValuePair("url", "www.somename.com"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
entity = response.getEntity();
System.out.println(entity.getContentType());
System.out.println(EntityUtils.getContentCharSet(entity));
System.out.println(EntityUtils.toString(entity));
我开始认为这是一个 apache 配置问题。我在 POST 方法的开头添加了一些调试语句,但我根本没有点击它们。这意味着 urls.py 文件有问题(我对此表示怀疑,因为它在 python 中工作)或者 apache 有问题。
请帮忙。提前致谢。
I've built a web service using Django-Piston that allows for POST and GET requests. As part of testing I wrote a quick Python script. Using the script I can successfully do both types of requests; however, when a client written in Java attempts to do a POST I get an error: "POST /api/service/ HTTP/1.1" 400 225 "-" "Apache-HttpClient/4.1 (java 1.5)"
My understanding is that http request message that is generated by any language has to be the same. In other words, if I test my web service using a python client and it works then it should work for all other languages that have a http library.
Here is the python code for the POST:
import urllib, urllib2
data = urllib.urlencode({'url': 'www.uvic.ca', 'name': 'uvic'})
url = 'http://xxx/api/service/'
req = urllib2.Request(url, data)
print urllib2.urlopen(req).read()
and here is the Java code:
HttpPost httpost = new HttpPost("http://xxx/api/service/");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("name", "some_name"));
nvps.add(new BasicNameValuePair("url", "www.somename.com"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
entity = response.getEntity();
System.out.println(entity.getContentType());
System.out.println(EntityUtils.getContentCharSet(entity));
System.out.println(EntityUtils.toString(entity));
I'm starting to think that this is an apache configuration problem. I put in some debugging statements at the start of my method for POST and I'm not hitting them at all. This means that something is wrong with the urls.py file (which I doubt because it works in python) or something is weird with apache.
Please help. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一点点搜索会对你有很大帮助。这是第一个谷歌结果。
http://weblog.mattdorn。 com/content/restful-web-apps-with-django-piston-and-ext-js/
这是谷歌的第二次回应。
https://bitbucket.org /jespern/django-piston/issue/99/bad-request-and-content-type-with-fix
A little search would help you a lot. This was the first Google result.
http://weblog.mattdorn.com/content/restful-web-apps-with-django-piston-and-ext-js/
This is the second Google response.
https://bitbucket.org/jespern/django-piston/issue/99/bad-request-and-content-type-with-fix