服务器之家

服务器之家 > 正文

详解java中通过post方式访问后台服务器

时间:2020-09-01 09:47     来源/作者:少壮不努力-老大敲代码

最近在学习java中通过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
Runnable payRunnable = new Runnable() 
      {
        @Override
        public void run() 
        {
          try
          {  
            HashMap<String, String> payParamMap = new HashMap<String, String>();
            payParamMap.put("canshu" ,"woshicanshu" );
            String payParamStr = XmlUtils.toXml(payParamMap);//转换成xml格式
             
            String resultStr = Utils.httpPost(UrlString, payParamStr);//调用访问函数
             
            //<span style="font-family: Arial, Helvetica, sans-serif;">resultStr 就是访问所得到的返回值 </span>
          }
          catch(Exception e)
          {
            e.printStackTrace();
          }
        }
      };
      // 必须异步调用
      Thread payThread = new Thread(payRunnable);
      payThread.start();

其次,上代码:

?
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
public class Utils {
  private static final String TAG = "woshiTag";
 
  public static String httpPost(String url, String entity) {
    if (url == null || url.length() == 0) {
      Log.e(TAG, "httpPost, url is null");
      return null;
    }
     
    HttpClient httpClient = getNewHttpClient();
     
    HttpPost httpPost = new HttpPost(url);
     
    try {
      httpPost.setEntity(new StringEntity(entity, HTTP.UTF_8));
      httpPost.setHeader("Accept", "application/json");
      httpPost.setHeader("Content-type", "application/json");
       
      HttpResponse resp = httpClient.execute(httpPost);
      if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        Log.e(TAG, "httpGet fail, status code = " + resp.getStatusLine().getStatusCode());
        return null;
      }
 
      return new String(EntityUtils.toByteArray(resp.getEntity()));
    } catch (Exception e) {
      Log.e(TAG, "httpPost exception, e = " + e.getMessage());
      e.printStackTrace();
      return null;
    }
  }
  private static HttpClient getNewHttpClient() { 
      try
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
        trustStore.load(null, null); 
 
        SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore); 
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 
 
        HttpParams params = new BasicHttpParams(); 
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); 
 
        SchemeRegistry registry = new SchemeRegistry(); 
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
        registry.register(new Scheme("https", sf, 443)); 
 
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); 
 
        return new DefaultHttpClient(ccm, params); 
      } catch (Exception e) { 
        return new DefaultHttpClient(); 
      
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/qq_22609555/article/details/65447130

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
iPhone12什么时候上市 iPhone12手机真实图片 苹果iphone12多少钱
iPhone12什么时候上市 iPhone12手机真实图片 苹果iphone12多少钱 2020-06-03
返回顶部