使用java的post方法向百度站长平台提交链接实例

内容目录
	public static String PostToBaidu(String Parameters)
	{
		URL url = null;
		HttpURLConnection http = null;
		String PostUrl = "http://data.zz.baidu.com/urls?site=so.xuji.pro&token=QHWjftajJ2yNsnyN";
		String result = "";
		try
		{
			url = new URL(PostUrl);
			http = (HttpURLConnection) url.openConnection();
			http.setDoInput(true);
			http.setDoOutput(true);
			http.setUseCaches(false);
			http.setConnectTimeout(50000);// 设置连接超时
			// 如果在建立连接之前超时期满,则会引发一个
			// java.net.SocketTimeoutException。超时时间为零表示无穷大超时。
			http.setReadTimeout(50000);// 设置读取超时
			// 如果在数据可读取之前超时期满,则会引发一个
			// java.net.SocketTimeoutException。超时时间为零表示无穷大超时。
			http.setRequestMethod("POST");
			// http.setRequestProperty("Content-Type","text/xml; charset=UTF-8");
			http.setRequestProperty("POST",
					"/urls?site=so.xuji.pro&token=QHWjftajJ2yNsnyN HTTP/1.1");
			http.setRequestProperty("User-Agent", "curl/7.12.1");
			http.setRequestProperty("Host", "data.zz.baidu.com");
			http.setRequestProperty("Content-Type", "text/plain");
			http.setRequestProperty("Content-Length", Parameters.getBytes().length + "");

			// 写入html内容信息
			OutputStreamWriter osw = new OutputStreamWriter(http.getOutputStream(), "utf-8");
			osw.write(Parameters);
			osw.flush();
			osw.close();
			http.connect();
			// System.out.println(http.getRequestProperties().);
			if (http.getResponseCode() == 200)
			{
				BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream(),
						"utf-8"));
				String inputLine;
				while ((inputLine = in.readLine()) != null)
				{
					result += inputLine;
				}
				in.close();
				// result = "["+result+"]";
			}
		} catch (Exception e)
		{
			System.out.println("err");
		} finally
		{
			if (http != null)
				http.disconnect();
		}
		return result;
	}

发表回复