zoukankan      html  css  js  c++  java
  • 远程访问

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(baseUrl + action);
    HttpResponse response = httpclient.execute(httpget);
    retStr = EntityUtils.toString(response.getEntity());

    JSONObject objRoot = new JSONObject(retStr );
    retStr = objRoot.getString("qwe");

    所用到的jar 包:httpcore httpclient
    http://www.cnblogs.com/Wen-yu-jing/p/3552154.html

    public static String sendPost(String host,String content,String encodeType) throws UnsupportedEncodingException,IOException
    {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost post= new HttpPost(host);

    //NameValuePair p = new BasicNameValuePair("v", "123");
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    list.add(new BasicNameValuePair("v", "1405322466697"));
    StringEntity entity = new UrlEncodedFormEntity(list);
    post.setEntity(entity);


    StringEntity myEntity;
    if(!encodeType.isEmpty())
    myEntity = new StringEntity(content,encodeType);
    else
    myEntity = new StringEntity(content);
    post.addHeader("Content-Type", "text/xml");
    post.addHeader("Connection","keep-alive");
    post.addHeader("Accept","*/*");
    post.addHeader("Cache-Control", "max-age=0");
    post.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
    post.setEntity(myEntity);
    HttpResponse response = httpClient.execute(post);
    String answerRes = "";
    if(response.getStatusLine().getStatusCode()==200)
    {
    //如果状态码为200,就是正常返回
    String result=EntityUtils.toString(response.getEntity());
    answerRes = new String(result.getBytes("iso-8859-1"), "UTF-8");
    }
    return answerRes;
    }

    public static String sendGet(String url) throws UnsupportedEncodingException,IOException
    {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet get= new HttpGet(url);
    get.addHeader("Connection","keep-alive");
    get.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/;q=0.8");
    get.addHeader("Cache-Control", "max-age=0");
    get.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
    HttpResponse response = httpClient.execute(get);
    String answerRes = "";
    if(response.getStatusLine().getStatusCode()==200)
    {
    //如果状态码为200,就是正常返回
    String result=EntityUtils.toString(response.getEntity());
    answerRes = new String(result.getBytes("iso-8859-1"), "UTF-8");
    }
    return answerRes;
    }

  • 相关阅读:
    文件下载
    字符串截取
    GAMES101 作业1
    GAMES101 作业0
    常见的企业管理系统有哪些
    Spring---AoP(面向切面编程)原理学习笔记【全】
    什么是OOP(Object Oriented Programming)面向对象编程
    Spring框架之控制反转IoC(Inversion of Control)的理解
    Typora编辑器MarkDown语法
    国民经济行业分类与代码(GB/T 4754-2017、GB/T 4754-2011、GB/T 4754-2002)
  • 原文地址:https://www.cnblogs.com/Wen-yu-jing/p/3767233.html
Copyright © 2011-2022 走看看