⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 get_request.java

📁 实现中国联通网站反向订购和退订的接口
💻 JAVA
字号:
package com.wireless.sms.sgip.http;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class Get_Request {

	/**
	 * 反向get请求,实现联通web反向订购/点播操作;
	 * @param requestURL = get请求目的及传送指令
	 * @param content = get包体内容,不同字段以==分割
	 */
    public static String request(String requestURL, String content) {
    	String resp = "";
        try {
           HttpConnectionManagerParams params = new HttpConnectionManagerParams();
           params.setDefaultMaxConnectionsPerHost(20);
           params.setMaxTotalConnections(30);
           HttpConnectionManager connmgr=new MultiThreadedHttpConnectionManager();
           HttpClient client = new HttpClient(connmgr);
           GetMethod method = new GetMethod(requestURL);
           String[] str = content.split("==");
//           System.out.println("content=" + content);
           if(str.length>=3) {
               NameValuePair[] d = {new NameValuePair("SPNumber", str[0]),
              		 new NameValuePair("AccessTime", str[1]),
              		 new NameValuePair("EncodeStr", str[2])};
               method.setQueryString(d);
           }
           method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
                      new DefaultHttpMethodRetryHandler());
           System.out.println("发送get请求:" + method.getURI());
           int statusCode = client.executeMethod(method);
           //读出返回内容
           java.io.BufferedInputStream in = new java.io.BufferedInputStream(method.getResponseBodyAsStream());
           int len = 1024;
           byte[] temp = new byte[len];
           int length = in.read(temp,0,len);
           byte[] total = (byte[])temp.clone();
           while(length != -1){
          	 temp = new byte[len];
          	 length = in.read(temp,0,len);
          	 ///////////////////////
          	 byte[] t = (byte[])total.clone();
          	 total = new byte[t.length+len];
          	 for(int i=0;i<t.length;i++){
          		 total[i]=t[i];
          	 }
          	 for(int i=0;i<len;i++){
          		 total[t.length+i]=temp[i];
          	 }
           }
           resp = new String(total).trim();
           if(statusCode != HttpStatus.SC_OK)
          	 System.out.println("request failed:" + method.getStatusLine());
//           System.out.println("statusCode =" + statusCode + " resp=" + resp + "\n zui=" + method.getQueryString());
           method.releaseConnection();
        } catch (Exception e) {
           e.printStackTrace();
        } finally {
        }
        return resp;
     }

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String url = "http://211.94.156.177/mazy?SPNumber=8009123&AccessTime=2007-10-09 18:09:53&EncodeStr=3tLZsTCT6R%2FndwmTg5NCL7qql9UgQQTbAjE3T%2BgfJ0HkdrWcaH0l3cZay19%2FfItx";
		Get_Request.request(url, "");
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -