我要用java打开google的搜索引擎进行搜索,实现方法是
exeplorer.exe http://www.google.com/search?hl=ja&q=aaaaaa&lr=
但是如果后面的参数过长的时候,比如说q=后面跟1000个字符,这时候用这个方法的话,url过长的部分将会被自动截掉,请问各位兄弟姐妹,除了上面的方法还有什么方法能让我打开搜索引擎并且超长的字符不会被截掉?非常着急,请各位多多帮忙
注: 开发的不是基于web的程序!!!!
exeplorer.exe http://www.google.com/search?hl=ja&q=aaaaaa&lr=
但是如果后面的参数过长的时候,比如说q=后面跟1000个字符,这时候用这个方法的话,url过长的部分将会被自动截掉,请问各位兄弟姐妹,除了上面的方法还有什么方法能让我打开搜索引擎并且超长的字符不会被截掉?非常着急,请各位多多帮忙
注: 开发的不是基于web的程序!!!!
你现在到底是在浏览器中打开页面还是在程序中发请求?
<center>
<form method=get action="http://www.google.com/search">
<table bgcolor="#FFFFFF"><tr><td>
<a href="http://www.google.com/intl/zh-CN/">
<img src="http://www.google.com/logos/Logo_40wht.gif"
border="0" alt="Google" align="absmiddle"></a>
<input type=text name=q size=31 maxlength=255 value="">
<input type=hidden name=ie value=GB2312>
<input type=hidden name=oe value=GB2312>
<input type=hidden name=hl value=zh-CN>
<input type=submit name=btnG value="Google 搜索">
</td></tr></table>
</form>
</center>
<!-- Search Google -->
boolean flag=false;
HttpURLConnection httpConn=null;
FileInputStream fin = null;
OutputStream out = null;
InputStreamReader isr = null;
BufferedReader in = null;
try {
//与商户建立一个连接
URL url = new URL(Url);
//打开连接
httpConn = (HttpURLConnection) url.openConnection();
fin = new FileInputStream(xmlFile2Send);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
copy(fin, bout);
byte[] b = bout.toByteArray();
httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
//以xml格式传送
httpConn.setRequestProperty("Content-Type", "text/xml; charset=GB2312");
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
out = httpConn.getOutputStream();
out.write(b);
out.flush();
httpConn.connect();
//获取输入流
isr = new InputStreamReader(httpConn.getInputStream());
in = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
String inputLine;
while ( (inputLine = in.readLine()) != null)
{
buf.append(inputLine);
}
if (buf.toString().trim().equalsIgnoreCase("ok")) {
Tools.trace(buf.toString().trim());
flag=true;
}
else {
flag=false;
}
}catch (MalformedURLException ex) {
Tools.error(ex);
}
catch (IOException ex) {
Tools.error(ex);
}
finally {
//关闭操作
Tools.closeBufferedReader(in);
Tools.closeInputStreamReader(isr);
Tools.closeOutputStream(out);
Tools.closeFileInputStream(fin);
Tools.closeHttpURLConnection(httpConn);
}
return flag;
}
这样我觉得不是很好的方案
能不能超出那楼主测试下就ok了阿
httpCon = (HttpURLConnection) url.openConnection(); httpCon.setRequestProperty("q=","123456");
我这样设完参数传回来的html代码竟然是google的首页,没有执行查询,这个url的参数应该怎么设阿,请hdhmail2000(禅剑飞雪)指点一下
//postpackage com.dingo.sendurl;
import java.net.*;
import java.io.InputStream;
import java.io.BufferedReader;import java.io.InputStreamReader;public class send_url_post {
private String urlStr;
private URL url;
private HttpURLConnection url_con;
private String response_content;
public void setUrlStr(String urlStr) {
this.urlStr = urlStr;
} public String getResponse_content() {
return response_content;
} private void setResponse_content(String response_content) {
this.response_content = response_content;
} public void send_url(String mobile_number){
try{
url = new url(/urlStr);
url_con=(HttpURLConnection)url.openConnection();
url_con.setRequestMethod("POST");
url_con.setDoOutput(true);
String param="action=mobile&mobile="+mobile_number;
url_con.getOutputStream().write(param.getBytes());
url_con.getOutputStream().flush();
url_con.getOutputStream().close(); InputStream in= url_con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in));
StringBuilder tempStr=new StringBuilder();
while(rd.read()!=-1){
tempStr.append(rd.readLine());
}
setResponse_content(new String(tempStr));
} catch(Exception e){
e.printStackTrace();
}
finally{
if(url_con!=null)
url_con.disconnect(); }
}
} //getpackage com.dingo.sendurl;import java.net.HttpURLConnection;
import java.net.URL;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;public class send_url_get {
private String urlStr;
private URL url;
private HttpURLConnection url_con;
private String contentStr;
public void setUrlStr(String urlStr) {
this.urlStr = urlStr;
} public String getContentStr() {
return contentStr;
} private void setContentStr(String contentStr) {
this.contentStr = contentStr;
} public void send_url(){
try{
StringBuilder temp = new StringBuilder();
url = new url(/urlStr); url_con= (HttpURLConnection)url.openConnection();
url_con.setDoOutput(true);
url_con.setRequestMethod("GET"); url_con.getOutputStream().flush();
url_con.getOutputStream().close();
InputStream in =url_con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in));
while(rd.read()!=-1){
temp.append(rd.readLine());
}
setContentStr(new String (temp)); } catch (Exception e){
e.printStackTrace();
} finally{
if(url_con!=null){
url_con.disconnect();
}
}
}
}
URL objUrl = new URL(strUrl);你用这个方式定义URL试试,直接把参数带在里面。
URL objUrl = new URL(strUrl);这样返回411号错误