好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java如何使用HTTPclient访问url获得数据

使用HTTPclient访问url获得数据

最近项目上有个小功能需要调用第三方的http接口取数据,用到了HTTPclient,算是做个笔记吧!

1、使用get方法取得数据

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

/**

  * 根据URL试用get方法取得返回的数据

  * @param url

  *        URL地址,参数直接挂在URL后面即可

  * @return

  */

public static String getGetDateByUrl(String url){ 

     String data = null ; 

     //构造HttpClient的实例   

     HttpClient httpClient = new HttpClient();   

     //创建GET方法的实例   

     GetMethod getMethod = new GetMethod(url);  

     //设置头信息:如果不设置User-Agent可能会报405,导致取不到数据 

     getMethod.setRequestHeader( "User-Agent" , "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:39.0) Gecko/20100101 Firefox/39.0" ); 

     //使用系统提供的默认的恢复策略   

     getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());   

     try { 

         //开始执行getMethod   

         int statusCode = httpClient.executeMethod(getMethod); 

         if (statusCode != HttpStatus.SC_OK) { 

             System.err.println( "Method failed:" + getMethod.getStatusLine()); 

         } 

         //读取内容 

         byte [] responseBody = getMethod.getResponseBody(); 

         //处理内容 

         data = new String(responseBody); 

     } catch (HttpException e){ 

         //发生异常,可能是协议不对或者返回的内容有问题 

         System.out.println( "Please check your provided http address!" ); 

         data = "" ; 

         e.printStackTrace(); 

     } catch (IOException e){ 

         //发生网络异常 

         data = "" ; 

         e.printStackTrace(); 

     } finally { 

         //释放连接 

         getMethod.releaseConnection(); 

     } 

     return data; 

}

2、使用POST方法取得数据

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

/**

  * 根据post方法取得返回数据

  * @param url

  *          URL地址

  * @param array

  *          需要以post形式提交的参数

  * @return

  */

public static String getPostDateByUrl(String url,Map<String ,String> array){ 

     String data = null ; 

     //构造HttpClient的实例   

     HttpClient httpClient = new HttpClient();   

     //创建post方法的实例   

     PostMethod postMethod = new PostMethod(url);  

     //设置头信息 

     postMethod.setRequestHeader( "User-Agent" , "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:39.0) Gecko/20100101 Firefox/39.0" ); 

     postMethod.addRequestHeader( "Content-Type" , "application/x-www-form-urlencoded;charset=utf-8" ); 

     //遍历设置要提交的参数 

     Iterator it = array.entrySet().iterator();   

     while (it.hasNext()){ 

         Map.Entry<String,String> entry =(Map.Entry) it.next();   

         String key = entry.getKey();   

         String value = entry.getValue().trim();   

         postMethod.setParameter(key,value); 

     } 

     //使用系统提供的默认的恢复策略   

     postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());   

     try { 

         //执行postMethod   

         int statusCode = httpClient.executeMethod(postMethod); 

         if (statusCode != HttpStatus.SC_OK) { 

             System.err.println( "Method failed:" + postMethod.getStatusLine()); 

         } 

         //读取内容 

         byte [] responseBody = postMethod.getResponseBody(); 

         //处理内容 

         data = new String(responseBody); 

     } catch (HttpException e){ 

         //发生致命的异常,可能是协议不对或者返回的内容有问题 

         System.out.println( "Please check your provided http address!" ); 

         data = "" ; 

         e.printStackTrace(); 

     } catch (IOException e){ 

         //发生网络异常 

         data = "" ; 

         e.printStackTrace(); 

     } finally { 

         //释放连接 

         postMethod.releaseConnection(); 

     } 

     System.out.println(data); 

     return data; 

}

使用httpclient后台调用url方式

使用httpclient调用后台的时候接收url类型的参数需要使用UrlDecoder解码,调用的时候需要对参数使用UrlEncoder对参数进行编码,然后调用。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

@SuppressWarnings ( "deprecation" )

     @RequestMapping (value = "/wechatSigns" , produces = "application/json;charset=utf-8" )

     @ResponseBody

     public String wechatSigns(HttpServletRequest request, String p6, String p13) {

         Map<String, String> ret = new HashMap<String, String>();

         try {

             System.out.println( "*****************************************p6:" +p6);

             URLDecoder.decode(p13);

             System.out.println( "*****************************************p13:" +p13);

             String p10 = "{\"p1\":\"1\",\"p2\":\"\",\"p6\":\"" + p6 + "\",\"p13\":\"" + p13 + "\"}" ;

             p10 = URLEncoder.encode(p10, "utf-8" );

             String url = WebserviceUtil.getGetSignatureUrl() + "?p10=" + p10;

             String result = WebConnectionUtil.sendGetRequest(url);

             JSONObject fromObject = JSONObject.fromObject(URLDecoder.decode(result, "utf-8" ));

             System.out.println(fromObject);

             String resultCode = JSONObject.fromObject(fromObject.getString( "meta" )).getString( "result" );

             if ( "0" .equals(resultCode)) {

                 JSONObject fromObject2 = JSONObject.fromObject(fromObject.get( "data" ));

                 String timestamp = fromObject2.getString( "timestamp" );

                 String appId = fromObject2.getString( "appId" );

                 String nonceStr = fromObject2.getString( "nonceStr" );

                 String signature = fromObject2.getString( "signature" );

                 ret.put( "timestamp" , timestamp);

                 ret.put( "appId" , appId);

                 ret.put( "nonceStr" , nonceStr);

                 ret.put( "signature" , signature);

                 JSONObject jo = JSONObject.fromObject(ret);

                 return ResultJsonBean.success(jo.toString());

             } else {

                 String resultMsg = JSONObject.fromObject(fromObject.getString( "meta" )).getString( "errMsg" );

                 return ResultJsonBean.fail(ConnectOauthCodeInfo.REQ_WECHATTOCKEN_CODE, resultMsg, "" );

             }

         } catch (Exception e) {

             logger.error(e, e);

             return ResultJsonBean.fail(ConnectOauthCodeInfo.REQ_WECHATREQERROE_CODE,

                     ConnectOauthCodeInfo.REQ_WECHATREQERROE_CODE, "" );

         }

     }

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

<pre name= "code" class = "java" > package com.dcits.djk.core.util;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.security.cert.CertificateException;

import java.security.cert.X509Certificate;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Set;

 

import javax.net.ssl.HostnameVerifier;

import javax.net.ssl.SSLContext;

import javax.net.ssl.SSLSession;

import javax.net.ssl.TrustManager;

import javax.net.ssl.X509TrustManager;

 

import org.apache.http.HttpEntity;

import org.apache.http.NameValuePair;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.entity.mime.MultipartEntityBuilder;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

import net.sf.json.JSONObject;

 

public class WebConnectionUtil {   

     public static String sendPostRequest(String url,Map paramMap,String userId){

         CloseableHttpClient httpclient= null ;

         CloseableHttpResponse response= null ;       

         try {

             httpclient = HttpClients.createDefault();

             HttpPost httppost = new HttpPost(url);

             List<NameValuePair> formparams = new ArrayList<NameValuePair>();

             if (userId != null || "" .equals(userId) ){

                 formparams.add( new BasicNameValuePair( "userId" ,userId));

             }

             Set keSet=paramMap.entrySet();

             for (Iterator itr=keSet.iterator();itr.hasNext();){

                 Map.Entry me=(Map.Entry)itr.next();

                 Object key=me.getKey();

                 Object valueObj=me.getValue();

                 String[] value= new String[ 1 ];

                 if (valueObj == null ){

                     value[ 0 ] = "" ;

                 } else {

                     if (valueObj instanceof String[]){ 

                         value=(String[])valueObj; 

                     } else { 

                         value[ 0 ]=valueObj.toString(); 

                     }

                 }

                 for ( int k= 0 ;k<value.length;k++){ 

                     formparams.add( new BasicNameValuePair(key.toString(),StringUtil.filterSpecialChar(value[k])));

                 }

             }

             UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8" );

             httppost.setEntity(uefEntity);

 

             response = httpclient.execute(httppost);

             if (response.getStatusLine().getStatusCode() == 200 ){

                 HttpEntity entity = response.getEntity();

                 String resultInfo = EntityUtils.toString(entity, "UTF-8" );

                 return resultInfo;

             } else {

                 return response.getStatusLine().getStatusCode() + "" ;

             }

         } catch (Exception e){

             e.printStackTrace();

         } finally {

             try {

                 if (httpclient != null ){

                     httpclient.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

             try {

                 if (response != null ){

                     response.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

         }

         return "404" ;

     }

    

     public static String sendPostRequest(String url,Map paramMap){

         CloseableHttpClient httpclient= null ;

         CloseableHttpResponse response= null ;

        

         try {

             httpclient = HttpClients.createDefault();

             HttpPost httppost = new HttpPost(url);

             List<NameValuePair> formparams = new ArrayList<NameValuePair>();

             Set keSet=paramMap.entrySet();

             for (Iterator itr=keSet.iterator();itr.hasNext();){

                 Map.Entry me=(Map.Entry)itr.next();

                 Object key=me.getKey();

                 Object valueObj=me.getValue();

                 String[] value= new String[ 1 ];

                 if (valueObj == null ){

                     value[ 0 ] = "" ;

                 } else {

                     if (valueObj instanceof String[]){ 

                         value=(String[])valueObj; 

                     } else { 

                         value[ 0 ]=valueObj.toString(); 

                     }

                 }

                 for ( int k= 0 ;k<value.length;k++){ 

                     formparams.add( new BasicNameValuePair(key.toString(),StringUtil.filterSpecialChar(value[k])));

                 }

             }

             UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8" );

             httppost.setEntity(uefEntity);

 

             response = httpclient.execute(httppost);

             if (response.getStatusLine().getStatusCode() == 200 ){

                 HttpEntity entity = response.getEntity();

                 String resultInfo = EntityUtils.toString(entity, "UTF-8" );

                 return resultInfo;

             } else {

                 return response.getStatusLine().getStatusCode() + "" ;

             }

         } catch (Exception e){

             e.printStackTrace();

         } finally {

             try {

                 if (httpclient != null ){

                     httpclient.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

             try {

                 if (response != null ){

                     response.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

         }

         return "404" ;

     }  

    

     public static String downloadFileToImgService(String remoteUtl,String imgUrl,String tempUrl){

         CloseableHttpClient httpclientRemote= null ;

         CloseableHttpResponse responseRemote= null ;     

         CloseableHttpClient httpclientImg= null ;

         CloseableHttpResponse responseImg= null ;    

         try {

             httpclientRemote = HttpClients.createDefault();

             HttpGet httpgetRemote = new HttpGet(remoteUtl);

            

             responseRemote = httpclientRemote.execute(httpgetRemote);

             if (responseRemote.getStatusLine().getStatusCode() != 200 ){

                 return "" ;

             }

             HttpEntity resEntityRemote = responseRemote.getEntity();

             InputStream isRemote = resEntityRemote.getContent();

             //写入文件

             File file = null ;

            

             boolean isDownSuccess = true ;

             BufferedOutputStream bos = null ;

             try {

                 BufferedInputStream bif = new BufferedInputStream(isRemote);

                 byte bf[] = new byte [ 28 ];

                 bif.read(bf);

                 String suffix = FileTypeUtil.getSuffix(bf);

                 file = new File(tempUrl + "/" + UuidUtil.get32Uuid()+suffix);

                 bos = new BufferedOutputStream( new FileOutputStream(file));

                 if (!file.exists()){

                     file.createNewFile();

                 }

                 bos.write(bf, 0 , 28 );

                 byte b[] = new byte [ 1024 * 3 ];

                 int len = 0 ;

                 while ((len=bif.read(b)) != - 1 ){

                     bos.write(b, 0 , len);

                 }

             } catch (Exception e){

                 e.printStackTrace();

                 isDownSuccess = false ;

             } finally {

                 try {

                     if (bos != null ){

                         bos.close();

                     }

                 } catch (Exception e) {

                     e.printStackTrace();

                 }

             }

             if (!isDownSuccess){

                 return "" ;

             }

            

             httpclientImg = HttpClients.createDefault();

             HttpPost httpPostImg = new HttpPost(imgUrl);

             MultipartEntityBuilder requestEntity = MultipartEntityBuilder.create();

             requestEntity.addBinaryBody( "userFile" , file);

            

             HttpEntity httprequestImgEntity = requestEntity.build();

             httpPostImg.setEntity(httprequestImgEntity);

             responseImg = httpclientImg.execute(httpPostImg);

             if (responseImg.getStatusLine().getStatusCode() != 200 ){

                 return "" ;

             }

             HttpEntity entity = responseImg.getEntity();

             String json = EntityUtils.toString(entity, "UTF-8" );

             json = json.replaceAll( "\"" , "" );

             String[] jsonMap = json.split( "," );

             String resultInfo = "" ;

             for ( int i = 0 ;i < jsonMap.length;i++){

                 String str = jsonMap[i];

                 if (str.startsWith( "url:" )){

                     resultInfo = str.substring( 4 );

                 } else if (str.startsWith( "{url:" )){

                     resultInfo = str.substring( 5 );

                 }

             }

             if (resultInfo.endsWith( "}" )){

                 resultInfo = resultInfo.substring( 0 ,resultInfo.length()- 1 );

             }

             try {

                 if (file != null ){

                     file.delete();

                 }

             } catch (Exception e){

                 e.printStackTrace();

             }

             return resultInfo;

         } catch (Exception e){

             e.printStackTrace();

         } finally {

             try {

                 if (httpclientRemote != null ){

                     httpclientRemote.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

             try {

                 if (responseRemote != null ){

                     responseRemote.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

             try {

                 if (httpclientImg != null ){

                     httpclientImg.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

             try {

                 if (responseImg != null ){

                     responseImg.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

         }

         return "" ;

     }

    

     public static String downloadFileToImgService(File file,String imgUrl){

         CloseableHttpClient httpclientImg= null ;

         CloseableHttpResponse responseImg= null ;    

         try {

             httpclientImg = HttpClients.createDefault();

             HttpPost httpPostImg = new HttpPost(imgUrl);

             MultipartEntityBuilder requestEntity = MultipartEntityBuilder.create();

             requestEntity.addBinaryBody( "userFile" , file);

            

             HttpEntity httprequestImgEntity = requestEntity.build();

             httpPostImg.setEntity(httprequestImgEntity);

             responseImg = httpclientImg.execute(httpPostImg);

             if (responseImg.getStatusLine().getStatusCode() != 200 ){

                 return "" ;

             }

             HttpEntity entity = responseImg.getEntity();

             String json = EntityUtils.toString(entity, "UTF-8" );

             json = json.replaceAll( "\"" , "" );

             String[] jsonMap = json.split( "," );

             String resultInfo = "" ;

             for ( int i = 0 ;i < jsonMap.length;i++){

                 String str = jsonMap[i];

                 if (str.startsWith( "url:" )){

                     resultInfo = str.substring( 4 );

                 } else if (str.startsWith( "{url:" )){

                     resultInfo = str.substring( 5 );

                 }

             }

             if (resultInfo.endsWith( "}" )){

                 resultInfo = resultInfo.substring( 0 ,resultInfo.length()- 1 );

             }

             return resultInfo;

         } catch (Exception e){

             e.printStackTrace();

         } finally {

             try {

                 if (httpclientImg != null ){

                     httpclientImg.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

             try {

                 if (responseImg != null ){

                     responseImg.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

         }

         return "" ;

     }

    

     public static String sendGetRequest(String url){

         CloseableHttpClient httpclient= null ;

         CloseableHttpResponse response= null ;

         try {

             httpclient = HttpClients.createDefault();

             HttpGet httpGet = new HttpGet(url);

 

             response = httpclient.execute(httpGet);

             if (response.getStatusLine().getStatusCode() == 200 ){

                 HttpEntity entity = response.getEntity();

                 String resultInfo = EntityUtils.toString(entity, "UTF-8" );

                 return resultInfo;

             } else {

                 return response.getStatusLine().getStatusCode() + "" ;

             }

         } catch (Exception e){

             e.printStackTrace();

         } finally {

             try {

                 if (httpclient != null ){

                     httpclient.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

             try {

                 if (response != null ){

                     response.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

         }

         return "404" ;

     }

    

     public static String sendHttpsPostRequestUseStream(String url,String param){

         CloseableHttpClient httpclient= null ;

         CloseableHttpResponse response= null ;       

         try {

             SSLContext ctx = SSLContext.getInstance( "TLS" );

             X509TrustManager xtm = new X509TrustManager() {

                 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                    

                 }

 

                 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                    

                 }

 

                 public X509Certificate[] getAcceptedIssuers() {

                     return null ;

                 }

             };

             TrustManager[] tm = {xtm};

             ctx.init( null ,tm, null );

             HostnameVerifier hv = new HostnameVerifier(){

                 public boolean verify(String arg0, SSLSession arg1) {

                     return true ;

                 }

             };

             httpclient = HttpClients.custom().setSSLHostnameVerifier(hv).setSSLContext(ctx).build();

             HttpPost httpPost = new HttpPost(url);

             StringEntity strEntity = new StringEntity(param, "utf-8" );

             httpPost.setEntity(strEntity);

            

             response = httpclient.execute(httpPost);

             if (response.getStatusLine().getStatusCode() == 200 ){

                 HttpEntity entity = response.getEntity();

                 String resultInfo = EntityUtils.toString(entity, "UTF-8" );

                 return resultInfo;

             } else {

                 return response.getStatusLine().getStatusCode() + "" ;

             }

         } catch (Exception e){

             e.printStackTrace();

         } finally {

             try {

                 if (httpclient != null ){

                     httpclient.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

             try {

                 if (response != null ){

                     response.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

         }

         return "404" ;

     }

    

     public static String sendHttpsPostRequestUseStream(String url,Map paramMap){

         CloseableHttpClient httpclient= null ;

         CloseableHttpResponse response= null ;       

         try {

             SSLContext ctx = SSLContext.getInstance( "TLS" );

             X509TrustManager xtm = new X509TrustManager() {

                 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                    

                 }

 

                 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                    

                 }

 

                 public X509Certificate[] getAcceptedIssuers() {

                     return null ;

                 }

             };

             TrustManager[] tm = {xtm};

             ctx.init( null ,tm, null );

             HostnameVerifier hv = new HostnameVerifier(){

                 public boolean verify(String arg0, SSLSession arg1) {

                     return true ;

                 }

             };

             httpclient = HttpClients.custom().setSSLHostnameVerifier(hv).setSSLContext(ctx).build();

             HttpPost httpPost = new HttpPost(url);

             List<NameValuePair> formparams = new ArrayList<NameValuePair>();

             Set keSet=paramMap.entrySet();

             for (Iterator itr=keSet.iterator();itr.hasNext();){

                 Map.Entry me=(Map.Entry)itr.next();

                 Object key=me.getKey();

                 Object valueObj=me.getValue();

                 String[] value= new String[ 1 ];

                 if (valueObj == null ){

                     value[ 0 ] = "" ;

                 } else {

                     if (valueObj instanceof String[]){ 

                         value=(String[])valueObj; 

                     } else { 

                         value[ 0 ]=valueObj.toString(); 

                     }

                 }

                 for ( int k= 0 ;k<value.length;k++){ 

                     formparams.add( new BasicNameValuePair(key.toString(),StringUtil.filterSpecialChar(value[k])));

                 }

             }

             UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8" );

             httpPost.setEntity(uefEntity);

            

             response = httpclient.execute(httpPost);

             if (response.getStatusLine().getStatusCode() == 200 ){

                 HttpEntity entity = response.getEntity();

                 String resultInfo = EntityUtils.toString(entity, "UTF-8" );

                 return resultInfo;

             } else {

                 return response.getStatusLine().getStatusCode() + "" ;

             }

         } catch (Exception e){

             e.printStackTrace();

         } finally {

             try {

                 if (httpclient != null ){

                     httpclient.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

             try {

                 if (response != null ){

                     response.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

         }

         return "404" ;

     }

    

     public static String sendHttpsGetRequestUseStream(String url){

         CloseableHttpClient httpclient= null ;

         CloseableHttpResponse response= null ;       

         try {

             SSLContext ctx = SSLContext.getInstance( "TLS" );

             X509TrustManager xtm = new X509TrustManager() {

                 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                    

                 }

 

                 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                    

                 }

 

                 public X509Certificate[] getAcceptedIssuers() {

                     return null ;

                 }

             };

             TrustManager[] tm = {xtm};

             ctx.init( null ,tm, null );

             HostnameVerifier hv = new HostnameVerifier(){

                 public boolean verify(String arg0, SSLSession arg1) {

                     return true ;

                 }

             };

             httpclient = HttpClients.custom().setSSLHostnameVerifier(hv).setSSLContext(ctx).build();

             HttpGet httpGet = new HttpGet(url);

            

             response = httpclient.execute(httpGet);

             if (response.getStatusLine().getStatusCode() == 200 ){

                 HttpEntity entity = response.getEntity();

                 String resultInfo = EntityUtils.toString(entity, "UTF-8" );

                 return resultInfo;

             } else {

                 return response.getStatusLine().getStatusCode() + "" ;

             }

         } catch (Exception e){

             e.printStackTrace();

         } finally {

             try {

                 if (httpclient != null ){

                     httpclient.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

             try {

                 if (response != null ){

                     response.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

         }

         return "404" ;

     }

    

     public static String sendHttpsPostRequestUseStreamForYZL(String url,OauthToken param){

         CloseableHttpClient httpclient= null ;

         CloseableHttpResponse response= null ;       

         try {

             SSLContext ctx = SSLContext.getInstance( "TLS" );

             X509TrustManager xtm = new X509TrustManager() {

                 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                    

                 }

 

                 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                    

                 }

 

                 public X509Certificate[] getAcceptedIssuers() {

                     return null ;

                 }

             };

             TrustManager[] tm = {xtm};

             ctx.init( null ,tm, null );

             HostnameVerifier hv = new HostnameVerifier(){

                 public boolean verify(String arg0, SSLSession arg1) {

                     return true ;

                 }

             };

             httpclient = HttpClients.custom().setSSLHostnameVerifier(hv).setSSLContext(ctx).build();

             HttpPost httpPost = new HttpPost(url);

             StringEntity strEntity = new StringEntity(param.toString(), "utf-8" );

             httpPost.setEntity(strEntity);

             httpPost.addHeader( "Content-Type" , "application/x-www-form-urlencoded" );

            

             response = httpclient.execute(httpPost);

             if (response.getStatusLine().getStatusCode() == 200 ){

                 HttpEntity entity = response.getEntity();

                 String resultInfo = EntityUtils.toString(entity, "UTF-8" );

                 return resultInfo;

             } else {

                 return response.getStatusLine().getStatusCode() + "" ;

             }

         } catch (Exception e){

             e.printStackTrace();

         } finally {

             try {

                 if (httpclient != null ){

                     httpclient.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

             try {

                 if (response != null ){

                     response.close();

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

         }

         return "404" ;

     }

}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://HdhCmsTestiteye测试数据/blog/quainter-2231502

查看更多关于Java如何使用HTTPclient访问url获得数据的详细内容...

  阅读:13次