怎么在java中使用common-httpclient包实现岗位请求

  介绍

怎么在java中使用common-httpclient包实现岗位请求?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

java的特点有哪些

java的特点有哪些   1. java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。   2. java具有简单性,面向对象,分布式、安全性,平台独立与可移植性,动态性等特点。   3.使用Java可以编写桌面应用程序,网络应用程序,分布式系统和嵌入式系统应用程序等。

import  java.io.BufferedReader;   import  java.io.FileOutputStream;   import  java.io.IOException;   import  java.io.InputStream;   import  java.io.InputStreamReader;   import  java.net.URL;   import  java.net.URLConnection;   import 并不知道;   import  java.util.Map;   import  java.util.zip.GZIPInputStream;      import  org.apache.commons.httpclient.HttpClient;   import  org.apache.commons.httpclient.HttpMethod;   import  org.apache.commons.httpclient.NameValuePair;   import  org.apache.commons.httpclient.methods.GetMethod;   import  org.apache.commons.httpclient.methods.PostMethod;   import  org.apache.commons.httpclient.methods.RequestEntity;   import  org.apache.commons.httpclient.methods.StringRequestEntity;   import  org.apache.commons.io.IOUtils;   import  org.slf4j.Logger;   import  org.slf4j.LoggerFactory;         public  class  HTTPUtils  {      ,private  static  Logger  Logger =, LoggerFactory.getLogger (HTTPUtils.class);/* *   *,文章才能请求   *,才能@param  url   *,才能@param  json   *才能@return   ,*/,public  static  String  postJosnContent (String  url, String  Json), throws  Exception  {//,,HttpPost  method =, new  HttpPost (url),,//,,DefaultHttpClient  httpClient =, new  DefaultHttpClient (),,//,,String  ret =,空;//,,try  {//,,,StringEntity  entity =, new  StringEntity (Json,“UTF-8");//解决中文乱码问题,,//,,,,entity.setContentEncoding (“UTF-8");,//,,,,entity.setContentType(“应用程序/json");//,,,,method.setEntity(实体),,//,,,,HttpResponse  result =, httpClient.execute(方法),,//,,,,ret =, EntityUtils.toString (result.getEntity ()),,//,,},catch  (Exception  e), {//,,,throw  e;//,,},{finally //,,,method.releaseConnection ();//,,}//,,return 回收;   logger.error才能(“请求接口参数:“,+,Json);   PostMethod 才能;method =, new  PostMethod (url);   HttpClient 才能;HttpClient =, new  HttpClient ();   try {才能   ,,RequestEntity  entity =, new  StringRequestEntity (Json,“应用程序/json"“UTF-8");   ,,method.setRequestEntity(实体);   ,,httpClient.executeMethod(方法);   ,,logger.error(“请求接口路径url:“, +, method.getURI () .toString ());   ,,InputStream 拷贝=,method.getResponseBodyAsStream ();   ,,//下面将流转换为字符串   ,,StringBuffer  sb =, new  StringBuffer ();   ,,InputStreamReader  isr =, new  InputStreamReader (,,“UTF-8");   ,,char [], b =, new  char [4096];   ,,(int  n, (=n  isr.read (b)), !=, 1;), {   ,,,sb.append (new 字符串(b,, 0,, n));   ,,}   ,,String  returnStr =, sb.toString ();   ,,return  returnStr;   ,,},catch  (Exception  e), {   ,,e.printStackTrace ();   ,,throw  e;   ,,},{finally    ,,method.releaseConnection ();   ,,}   ,}   }

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。

怎么在java中使用common-httpclient包实现岗位请求