2011-03-11 109 views
0

我已经使用允许POST和GET请求的Django-Piston构建了一个Web服务。作为测试的一部分,我写了一个快速的Python脚本。使用脚本,我可以成功执行这两种请求;但是,当用Java编写的客户端尝试执行POST时,出现错误:“POST/api/service/HTTP/1.1”400 225“ - ”“Apache-HttpClient/4.1(java 1.5)”Django-Piston和Python客户端与Java客户端

My理解是由任何语言生成的http请求消息必须是相同的。换句话说,如果我使用python客户端测试我的web服务并且它可以工作,那么它应该适用于所有其他具有http库的语言。

这里是这一职位的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中很奇怪。

请帮忙。提前致谢。

回答

2

一点搜索会帮助你很多。这是Google的第一个结果。

http://weblog.mattdorn.com/content/restful-web-apps-with-django-piston-and-ext-js/

The reason for the 400 errors is that ExtJS is appending a charset onto the Content-Type field, which causes piston to not interpret the Content-Type correcly. There is an open issue for it at http://bitbucket.org/jespern/django-piston/issue/121/content-type-is-not-being-split-against . I was able to get the example working after I applied the patch and did an easy_install.

这是第二个谷歌的回应。

https://bitbucket.org/jespern/django-piston/issue/99/bad-request-and-content-type-with-fix

+0

我碰到那些很好,但因为这些问题在那里解决一年前,我想我已经有最新的版本(服务器最近安装的)。我的utils.py文件与他们提到的相比也略有不同:if ctype == type_formencoded:return None。我如何去应用这个补丁。我尝试使用补丁--dry-run -p1 piston-121-for-v-0.2.2.patch,但它只是坐在那里(sry新手在这里)。 – Przemek 2011-03-11 23:52:13

+0

我最终手动进行了更改。我不确定这是否会影响未来的更新。感谢您的帮助,解决了这个问题。让我知道,如果我补丁的方式会在稍后导致我的问题。 – Przemek 2011-03-12 00:53:10