【发布时间】:2011-03-02 18:02:30
【问题描述】:
我想上传一个 txt 文件到一个网站,我承认我没有仔细研究过它,但我看过一些例子,希望有更多有经验的意见关于我是否要加入正确的方向。
这是我目前所拥有的:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
private String ret;
HttpResponse response = null;
HttpPost httpPost = null;
public String postPage(String url, String data, boolean returnAddr) {
ret = null;
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
httpPost = new HttpPost(url);
response = null;
StringEntity tmp = null;
try {
tmp = new StringEntity(data,"UTF-8");
} catch (UnsupportedEncodingException e) {
System.out.println("HTTPHelp : UnsupportedEncodingException : "+e);
}
httpPost.setEntity(tmp);
try {
response = httpClient.execute(httpPost,localContext);
} catch (ClientProtocolException e) {
System.out.println("HTTPHelp : ClientProtocolException : "+e);
} catch (IOException e) {
System.out.println("HTTPHelp : IOException : "+e);
}
ret = response.getStatusLine().toString();
return ret;
}
我这样称呼它:
postPage("http://www.testwebsite.com", "data/data/com.testxmlpost.xml/files/logging.txt", true));
我希望能够将文件从设备上传到网站。
但是当我尝试这种方式时,我得到了以下响应。
HTTP/1.1 405 Method Not Allowed
我是在尝试正确的方式还是应该换一种方式?
【问题讨论】:
标签: java android http upload http-post