用HttpClient(版本4.0)方式访问cxf-webservice,我用Get方式访问没有问题,但是改成Post方式就出错, import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; .......... .......... .......... public static void doPost(String httpURL, List<NameValuePair> params) throws IOException{ //生成一个post请求对象 HttpPost httpPost = new HttpPost(httpURL); HttpParams httpParams = new BasicHttpParams(); //设置超时30秒 HttpConnectionParams.setConnectionTimeout(httpParams, 10*3000); HttpConnectionParams.setSoTimeout(httpParams, 10*3000); DefaultHttpClient httpClient = new DefaultHttpClient(httpParams); //设置头信息 httpClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { if(!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip"); } } }); //对参数进行编码 httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); try { HttpResponse httpResponse = httpClient.execute(httpPost); //操作状态 if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ System.out.println("success>>>>"+ EntityUtils.toString(httpResponse.getEntity())); }else{ System.out.println("error>>>>>>>"+ httpResponse.getStatusLine().toString()); } } catch (SocketTimeoutException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args){ List <NameValuePair> nvps = new ArrayList <NameValuePair>(); nvps.add(new BasicNameValuePair("method", "delete")); nvps.add(new BasicNameValuePair("id","BBbbc0c00207a0bb")); nvps.add(newBasicNameValuePair("uid","1000")); doPost("http://localhost:8088/cxf-service/rest",nvps); } 运行结果(Java控制台):error>>>>>>>HTTP/1.1 405 Method Not Allowed tomcat控制台的错误信息如下: 这到底是神马原因,帮忙看看。THKS… |
|
50分 |
HTTP-GET的处理特征如下:
1.将数据添加到URL 2.利用一个问号(”?”)代表URL地址的结尾与数据的开端。 3.每一个数据的元素以 名称/值 (name/value) 的形式出现。 4.利用一个分号(“;”)来区分多个数据元素。 HTTP-POST的处理特征如下: 1.将数据包括在HTTP主体中。 2.同样的,数据的元素以 名称/值 (name/value) 的形式出现。 3.但是每一个数据元素分别占用主体的一行。 |
没人回答,只有自己顶了。就用get方式获取得了…
|
|
你检察一下你写的“@Consumes(MediaType.TEXT_XML)”,应该是MediaType不一样
|