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;
}
}
}