首页 > 程序开发 > 综合编程 > 其他综合 >

用java发送ajax请求实现刷票

2017-04-19

用java发送ajax请求实现刷票:用java发送ajax请求怎么实现刷票呢?希望下面的文章对大家有所帮助。

用java发送ajax请求实现刷票:用java发送ajax请求怎么实现刷票呢?希望下面的文章对大家有所帮助。
import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;

public class AjaxSend {

	private static String url = "https://172.26.188.112:8080/BooksStore/ajax/selectuser";
	
	public static void main(String[] args) {

		HttpClient client = new HttpClient();
		String body = "[{\"name\":liming,\"password\":123456}]";		

		method(client, url, body);
	}

	@SuppressWarnings("deprecation")
	public static void method(HttpClient client, String url, String body) {

		PostMethod method = new PostMethod(url);
		method.setRequestBody(body);// addParameters(postData);

		try {
			// Execute the method.
			int statusCode = client.executeMethod(method);

			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: " + method.getStatusLine());
			}

			// Read the response body.
			byte[] responseBody = method.getResponseBody();

			// Deal with the response.
			// Use caution: ensure correct character encoding and is not binary data
			System.out.println(new String(responseBody, "utf-8"));
		}
		catch (HttpException e) {
			System.err.println("Fatal protocol violation: " + e.getMessage());
			e.printStackTrace();
		}
		catch (IOException e) {
			System.err.println("Fatal transport error: " + e.getMessage());
			e.printStackTrace();
		}
		finally {
			// Release the connection.
			method.releaseConnection();
		}
	}

}

我在前台捕捉到的ajax是这样的:

$.ajax({
		type : "POST",
		url : ContextPath + urls.getXxl,
		data : {
			name:$("#user").val(),
			password:$("#password").val()
		},
		dataType : "json",
		async : false,
		cache : false,
		success : function(jsonData) {	
			if(jsonData.dataExit){
				window.location.href="xxx";
			}
			else{
				error();
			}
		}
	});	
相关文章
最新文章
热点推荐