如何设置标题使用http post方法
问题描述:
我目前正在一个应用程序.. 在该应用程序中我必须使用get和post两种方法.. get方法正常工作,但在post方法突然我得到的响应像如何设置标题使用http post方法
"invalid method.post required"..
我的代码是:
String list_url = "************************";
try {
String appand = TimeStampForAuth.TimeStameMethod(main.this);
String auth = android.util.Base64.encodeToString(("iphone" + ":" +
"2a5a262d5a")
.getBytes("UTF-8"), android.util.Base64.NO_WRAP);
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(list_url+appand);
Log.d("URL_LIST", list_url+appand);
List nvps = new ArrayList();
nvps.add(new BasicNameValuePair("login",Login));
nvps.add(new BasicNameValuePair("password",password));
nvps.add(new BasicNameValuePair("title",title));
nvps.add(new BasicNameValuePair("category_id",cat_id));
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
post.addHeader("Authorization", "Basic " + auth);
post.setEntity(p_entity);
HttpResponse response = client.execute(post);
HttpEntity responseEntity = response.getEntity();
String s = EntityUtils.toString(responseEntity);
Log.d("List response", s);
}
catch(Exception e){
System.out.println(e.toString());
}
在
我失踪somethinf或者是什么,我不know..is所有的东西是有效的POST方法....
请帮助我尽早...
感谢你.........
答
尝试使用setHeader()
代替addHeader()
答
你可以尝试的HttpClient 4.1 Android版:http://code.google.com/p/httpclientandroidlib/ (Android使用的HttpClient版本4.0中存在错误。) 您也可以使用httpClient.log.enableDebug(true)更多地调试它。
如果你不想使用外部库,我会开始调试网络流量。 使用名为Wireshark的软件,您可以轻松查看并检查所有Http请求/响应。
若需授权我个人会用:
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(hostname, port),
new UsernamePasswordCredentials(user, pass));
答
用这个代替:
HttpURLConnection connection = (HttpsURLConnection) serviceURL.openConnection();
connection.setRequestMethod("POST");
会不会是服务器端的问题?已经尝试用RESTClient-Addon启动请求? – Tima 2011-08-17 11:57:46
有没有问题从serverside..serverside请求发送作为获取请求使用此方法..是否有可能..请帮助我..我不知道这个代码出了什么问题.. – Kutbi 2011-08-18 05:12:06