https和http协议接口post请求接口方法

朱雀 2022-06-10 06:59 683阅读 0赞

一、只针对http 的post请求

package test;

import java.io.InputStreamReader;

import java.net.URI;

import net.sf.json.JSONObject;

import org.apache.http.Header;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

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

import org.apache.http.entity.StringEntity;

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

import org.apache.http.util.EntityUtils;

public class HttpClientRequest {

public static String post(String url,JSONObject json, String token){

String result = null;

  1. HttpClient client = new DefaultHttpClient();
  2. HttpPost request;
  3. try \{
  4. request = new HttpPost(new URI(url));
  5. if(json != null)\{
  6. StringEntity s = new StringEntity(json.toString(),"utf-8");

s.setContentType(“application/json”);

request.setEntity(s);

  1. \}
  2. if(token != null)\{
  3. request.setHeader("token", token);
  4. \}
  5. HttpResponse response = client.execute(request);
  6. int returncode = response.getStatusLine().getStatusCode();
  7. System.out.println("Response Code:"+returncode);
  8. if (returncode == 200) \{
  9. Header\[\] header = response.getHeaders("token");
  10. if(header.length >0)\{
  11. for(Header h:header)\{
  12. System.out.println(h.getName()+" "+h.getValue());
  13. \}
  14. \}
  15. HttpEntity entity = response.getEntity();
  16. long contentLen = entity.getContentLength();
  17. if(contentLen == 0)\{
  18. return "User Has No Authority";
  19. \}

String charset = EntityUtils.getContentCharSet(entity);

InputStreamReader isr = new InputStreamReader(entity.getContent(), charset);

StringBuffer sb = new StringBuffer();

char[] ct = new char[64];

int len = 0;

while((len = isr.read(ct))!=-1){

String sst = new String(ct);

sst = sst.substring(0, len).trim();

sb.append(sst);

}

result = sb.toString().replace(“\\\“”, “\“”);

result = result.substring(1, result.length()-1);

  1. \}
  2. \} catch(Exception e) \{
  3. e.printStackTrace();
  4. \}
  5. return result;

}

}

二、针对https和http都可以的post请求

package https;

import java.io.BufferedReader;

import java.io.ByteArrayOutputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.net.FileNameMap;

import java.net.MalformedURLException;

import java.net.URI;

import java.net.URISyntaxException;

import java.net.URL;

import java.security.KeyManagementException;

import java.security.NoSuchAlgorithmException;

import java.security.NoSuchProviderException;

import java.security.cert.CertificateException;

import java.security.cert.X509Certificate;

import java.util.List;

import java.util.Map;

import javax.net.ssl.HostnameVerifier;

import javax.net.ssl.HttpsURLConnection;

import javax.net.ssl.SSLContext;

import javax.net.ssl.SSLSession;

import javax.net.ssl.TrustManager;

import javax.net.ssl.X509TrustManager;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.ProtocolException;

import org.apache.commons.httpclient.methods.PostMethod;

import org.apache.http.Header;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

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

import org.apache.http.conn.scheme.Scheme;

import org.apache.http.conn.ssl.SSLSocketFactory;

import org.apache.http.entity.HttpEntityWrapper;

import org.apache.http.entity.StringEntity;

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

import org.apache.http.util.EntityUtils;

public class HttpsUtil{

static class TrustAnyTrustManager implements X509TrustManager{

@Override

public void checkClientTrusted(X509Certificate[] arg0, String arg1)

throws CertificateException {

// TODO Auto-generated method stub

}

@Override

public void checkServerTrusted(X509Certificate[] arg0, String arg1)

throws CertificateException {

// TODO Auto-generated method stub

}

@Override

public X509Certificate[] getAcceptedIssuers() {

// TODO Auto-generated method stub

return null;

}

}

static class TrustAnyHostnameVerifier implements HostnameVerifier{

@Override

public boolean verify(String arg0, SSLSession arg1) {

// TODO Auto-generated method stub

return true;

}

}

/**

* post方式请求服务器(https协议)

* @param url

* 请求地址

* @param content

* 参数

* @param charset

* 编码

* @return

* @throws URISyntaxException

* @throws NoSuchAlgorithmException

* @throws KeyManagementException

* @throws IOException

*/

static public String SendHttpsPOST(String url,JSONObject json, String token) throws URISyntaxException

{

String result = null;

  1. HttpClient client = new DefaultHttpClient();
  2. HttpPost request;
  3. try \{
  4. request = new HttpPost(new URI(url));
  5. if(json != null)\{
  6. StringEntity s = new StringEntity(json.toString(),"utf-8");

s.setContentType(“application/json”);

request.setEntity(s);

  1. \}
  2. if(token != null)\{
  3. request.setHeader("token", token);
  4. \}
  5. //设置SSLContext
  6. SSLContext sslcontext = SSLContext.getInstance("TLS");
  7. sslcontext.init(null, new TrustManager\[\]\{new TrustAnyTrustManager()\}, null);
  8. //打开连接
  9. //要发送的POST请求url?Key=Value&Key2=Value2&Key3=Value3的形式
  10. SSLSocketFactory socketFactory = new SSLSocketFactory(sslcontext);
  11. client.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, socketFactory));
  12. HttpResponse response = client.execute(request);
  13. int returncode = response.getStatusLine().getStatusCode();
  14. System.out.println("Response Code:"+returncode);
  15. if (returncode == 200) \{
  16. Header\[\] header = response.getHeaders("token");
  17. if(header.length >0)\{
  18. for(Header h:header)\{
  19. System.out.println(h.getName()+" "+h.getValue());
  20. \}
  21. \}
  22. HttpEntity entity = response.getEntity();
  23. long contentLen = entity.getContentLength();
  24. if(contentLen == 0)\{
  25. return "User Has No Authority";
  26. \}

String charset = EntityUtils.getContentCharSet(entity);

InputStreamReader isr = new InputStreamReader(entity.getContent(), charset);

StringBuffer sb = new StringBuffer();

char[] ct = new char[64];

int len = 0;

while((len = isr.read(ct))!=-1){

String sst = new String(ct);

sst = sst.substring(0, len).trim();

sb.append(sst);

}

result = sb.toString().replace(“\\\“”, “\“”);

result = result.substring(1, result.length()-1);

  1. \}
  2. \} catch (KeyManagementException e) \{
  3. e.printStackTrace();
  4. \} catch (NoSuchAlgorithmException e) \{
  5. e.printStackTrace();
  6. \} catch (MalformedURLException e) \{
  7. e.printStackTrace();
  8. \} catch (ProtocolException e) \{
  9. e.printStackTrace();
  10. \} catch (IOException e) \{
  11. e.printStackTrace();
  12. \}
  13. return result;

}

}

发表评论

表情:
评论列表 (有 0 条评论,683人围观)

还没有评论,来说两句吧...

相关阅读

    相关 关于HTTP协议GET、POST请求

    1、什么是通信协议?     计算机A和计算机B之间在传送数据之前,制定好的一种数据传送格式。     发送数据的时候采用特定的格式发送,接收方提前知道数据的格式,可以