java如何调用对方http接口 新手虚心求教

java如何调用对方http接口 新手虚心求教,第1张

/**

* 程序中访问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 = "*/*"


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12145243.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存