* 程序中访问http数据接口
*/
public static String getURLContent(String urlStr) {
/** 网络的url地址 */
URL url = null
/** http连接 */
HttpURLConnection httpConn = null
/**//** 输入流 */
BufferedReader in = null
StringBuffer sb = new StringBuffer()
try {
url = new URL(urlStr)
in = new BufferedReader(new InputStreamReader(url.openStream(), "GBK"))
String str = null
while ((str = in.readLine()) != null) {
sb.append(str)
}
} catch (Exception ex) {
} finally {
try {
if (in != null) {
in.close()
}
} catch (IOException ex) {
}
}
String result = sb.toString()
System.out.println(result)
return result
}
string url = "你的地址"string html = ""
try
{
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest
req.ContentType = "multipart/form-data"
req.Accept = "*/*"
req.UserAgent = "Mozilla/4.0 (compatibleMSIE 6.0Windows NT 5.1SV1.NET CLR 2.0.50727)"
req.Timeout = 30000//30秒连接不成功就中断
req.Method = "GET"
HttpWebResponse response = req.GetResponse() as HttpWebResponse
using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
html = sr.ReadToEnd()
}
}
catch (WebException ex)
{
Console.WriteLine(ex.ToString())
}
Console.WriteLine(html)
/// <summary>/// 发起一个HTTP请求(以POST方式)
/// </summary>
/// <param name="url"></param>
/// <param name="param"></param>
/// <returns></returns>
public static string HttpPost(string url, string param = "")
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.Accept = "*/*"
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)