javapost模拟
‘壹’ java POST xml请求
是的,就是将你的xml文本内容写出去就好了,接收端接收到后解析xml 以取得参数名及值
‘贰’ java模拟post 出现“可能访问过于频繁或非正常访问”
应该是有限制,把你的post请求,加上浏览器代理信息等,尽量看起来更像一个浏览器发起的正常请求。
‘叁’ 怎么使用java模拟post请求
你要导入httpclient的jar包,要是你请求参数格式是json的或者返回的是json格式数据,你还需要导入json包
/**
* post请求
* @param url url地址
* @param jsonParam 参数
* @param noNeedResponse 不需要返回结果
* @return
*/
public static JSONObject httpPost(String url,JSONObject jsonParam, boolean noNeedResponse){
//post请求返回结果
DefaultHttpClient httpClient = new DefaultHttpClient();
JSONObject jsonResult = null;
‘肆’ 如何使用java模拟post请求
有很多工具可以实现,比如使用HTTPclient工具去模拟post请求
‘伍’ 要求将数据库中获取的一条数据上报成功,用java模拟 post请求求大神给点例子啊或者思路啊。解决必采纳
import org.apache.commons.httpclient.methods.PostMethod;
String url="";
PostMethod pm = new PostMethod(url);
pm.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=GBK");
pm.setRequestHeader(....);
.....//设置请亮野纤求头信息
pm.addParameter(String,Object);
....//设置参数
this.executeMethod(pm);//发送请求敬仿
String rt = toString(pm.getResponseBodyAsStream(),"脊猜GBK");//解析响应
‘陆’ 关于JAVA模拟发送post请求并响应内容
如果你是用java的api实现的模拟post请求,那么你需要在你之前构造的http request的header里加上
Cookie:名字=值 然后统一包装成你的conenction的OutputStream。
建议你用apache的HttpClient api项目,里面有专门处理cookie的api,这样事情就简单许多。
‘柒’ 如何使用java模拟post请求
两种选择:一、使用httpclient,二使用java自带的类库。
1、java自带类库:
public static String call(String address,String params) {
URL url = null;
HttpURLConnection httpurlconnection = null;
StringBuilder result = new StringBuilder();
try {
url = new URL(address);
// 以post方式请求
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
if(null!=params&¶ms.length()>0){
httpurlconnection.getOutputStream().write(params.getBytes());
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
}
// 获取页面内容
java.io.InputStream in = httpurlconnection.getInputStream();
java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in, Config.DEFAULT_CHARSET));
String str = breader.readLine();
while (str != null) {
result.append(str);
str = breader.readLine();
}
breader.close();
in.close();
} catch (Exception e) {
} finally {
if (httpurlconnection != null)
httpurlconnection.disconnect();
}
return result.toString().trim();
}
2、httpclient:
public static String post(String url,String params){
HttpClient httpClient = new DefaultHttpClient();
StringBuilder builder = new StringBuilder();
HttpPost post = new HttpPost(url);
try {
if(null!=params){
post.setEntity(new StringEntity(params,"UTF-8"));
}
HttpResponse resp = httpClient.execute(post);
int statusCode = resp.getStatusLine().getStatusCode();
if(statusCode<=304){
HttpEntity entity = resp.getEntity();
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
}
int i = (int)entity.getContentLength();
i = i<0 ? 4096 : i;
final InputStream instream = entity.getContent();
final Reader reader = new InputStreamReader(instream, Config.DEFAULT_CHARSET);
final CharArrayBuffer buffer = new CharArrayBuffer(i);
final char[] tmp = new char[1024];
int l;
while((l = reader.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
builder.append(buffer);
}
post.abort();
} catch (Exception e) {
post.abort();
}
return builder.toString().trim();
}
‘捌’ java怎么模拟post提交
String action = "xxxxxxxxxxx";
URL url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setConnectTimeout(0);
http.setInstanceFollowRedirects(true);
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDefaultUseCaches(false);
http.setDoOutput(true);
String queryString = "";
PrintWriter out = new PrintWriter(http.getOutputStream());
out.print(queryString);//传入参数
out.close();
http.connect();//连接
InputStream in = httpURLConnection.getInputStream();//返回流
其他的自己来了吧
‘玖’ java 模拟post登录
这个要分两步,先用GET方法取得页面源码,分析出mpc的值,然后用POST方法发送数据就能登录了。当然一切工作之前要设置好CookieHandler
post页面:
http://www.songtaste.com/info_oper.php?tag=signin&pageref=
post参数就4个而已,
name=yourName&pwd=yourPassword&B12=Login&mpc=分析得到的mpc
看如下例子:
http://..com/question/141336096.html
将这个例子中的如下语句改一下就能收到数据的
connection.getInputStream().close();
//
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SongTaste {
private static String baseURL="http://www.songtaste.com";
private static String loginURL=baseURL+"/signin.php";
private static String actionURL=baseURL+"/info_oper.php?tag=signin&pageref=";
private static String musicURL=baseURL+"/music/";
private static CookieManager cm;
static{
cm=new CookieManager();
cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cm);
}
public static void main(String[] args) throws Exception{
// HttpURLConnection.setFollowRedirects(true);
login("abcmusic","000000");
listContents();
}
private static void listContents() throws Exception {
byte[] dat=new HttpGet(musicURL).data;
String src=new String(dat,"gbk");
Matcher m=Pattern.compile("MSL\\(.*?\\)").matcher(src);
while(m.find())
System.out.println(m.group());
}
private static void login(String name,String pwd){
byte[] dat=new HttpGet(loginURL).data;
String src=new String(dat);
Matcher m=Pattern.compile("name=mpc.*?>").matcher(src);
String mpc="";
if(m.find()){
mpc=m.group();
// System.out.println(mpc);
mpc=mpc.substring(15,mpc.length()-1);
// System.out.println(mpc);
}
//do login
new HttpPost(actionURL,String.format("name=%s&pwd=%s&B12=Login&mpc=%s",name,pwd,mpc));
}
private static class HttpGet extends Thread{
private static final int bufferSize=1024;
private String ustr;
private byte[] data;
private HttpGet(String u,String...ref){
ustr=u;
start();
try {join();} catch (Exception e) {}
}
public void run(){
try{
URL u = new URL(ustr);
HttpURLConnection uc=(HttpURLConnection)u.openConnection();
byte[] b={};
byte[] t=new byte[bufferSize];
int r;
BufferedInputStream bin=new BufferedInputStream(uc.getInputStream());
while((r=bin.read(t))>-1){
b=putData(b,t,r);
}
bin.close();
uc.disconnect();
data=b;
}catch(Exception e){}
}
private final byte[] putData(byte[] b, byte[] t, int r) {
byte[] tb=new byte[b.length+r];
System.array(b, 0, tb, 0, b.length);
System.array(t, 0, tb, b.length, r);
return tb;
}
}
private static class HttpPost extends Thread{
private static int blen=1024;
private static String contentType="application/x-www-form-urlencoded";
private String url,pms;
private byte[] dat={};
private HttpPost(String u,String p){
url=u;
pms=p;
start();
try{join();}catch(Exception e){}
}
public void run(){
try{
URL u = new URL(url);
HttpURLConnection connection=(HttpURLConnection)u.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",contentType);
connection.setRequestProperty("Content-Length",String.valueOf(pms.length()));
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
DataOutputStream dout=new DataOutputStream(connection.getOutputStream());
dout.write(pms.getBytes());
dout.flush();
dout.close();
InputStream in=connection.getInputStream();
BufferedInputStream bin=new BufferedInputStream(in);
byte[] buff=new byte[blen],bs={};
int r;
while((r=bin.read(buff))>-1){
bs=putData(bs,buff,r);
}
bin.close();
connection.disconnect();
dat=bs;
}catch(Exception e){}
}
private final byte[] putData(byte[] b, byte[] t, int r) {
byte[] tb=new byte[b.length+r];
System.array(b, 0, tb, 0, b.length);
System.array(t, 0, tb, b.length, r);
return tb;
}
}
}