asp. net 배경 으로 HTTP 요청 보 내기
///
///
///
/// “name= &pass=123456”
///
public static string RequestWebAPI(string url, string sendData)
{
string backMsg = "";
try
{
System.Net.WebRequest httpRquest = System.Net.HttpWebRequest.Create(url);
httpRquest.Method = "POST";
// , ContentType
httpRquest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
byte[] dataArray = System.Text.Encoding.UTF8.GetBytes(sendData);
//httpRquest.ContentLength = dataArray.Length;
System.IO.Stream requestStream = null;
if (string.IsNullOrWhiteSpace(sendData) == false)
{
requestStream = httpRquest.GetRequestStream();
requestStream.Write(dataArray, , dataArray.Length);
requestStream.Close();
}
System.Net.WebResponse response = httpRquest.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.UTF8);
backMsg = reader.ReadToEnd();
reader.Close();
reader.Dispose();
requestStream.Dispose();
responseStream.Close();
responseStream.Dispose();
}
catch (Exception)
{
throw;
}
return backMsg;
}
2. HttpClient 방식 (다음으로 전환:http://blog.csdn.net/qianmenfei/article/details/37974767)
1. 공통 http 클래스
using System;
using System.Globalization;
using System.Net;
using System.Text;
namespace Test
{
public class HttpCommon
{
///
/// Http Get
///
/// Url
/// ( UTF8)
///
public static string HttpGet(string url, Encoding encode = null)
{
string result;
try
{
var webClient = new WebClient { Encoding = Encoding.UTF8 };
if (encode != null)
webClient.Encoding = encode;
result = webClient.DownloadString(url);
}
catch (Exception ex)
{
result = ex.Message;
}
return result;
}
///
/// Http Get
///
/// Url
///
/// ( UTF8)
public static void HttpGetAsync(string url,
DownloadStringCompletedEventHandler callBackDownStringCompleted = null, Encoding encode = null)
{
var webClient = new WebClient { Encoding = Encoding.UTF8 };
if (encode != null)
webClient.Encoding = encode;
if (callBackDownStringCompleted != null)
webClient.DownloadStringCompleted += callBackDownStringCompleted;
webClient.DownloadStringAsync(new Uri(url));
}
///
/// Http Post
///
/// Url
/// Url
/// ( UTF8)
///
public static string HttpPost(string url, string postStr = "", Encoding encode = null)
{
string result;
try
{
var webClient = new WebClient { Encoding = Encoding.UTF8 };
if (encode != null)
webClient.Encoding = encode;
var sendData = Encoding.GetEncoding("GB2312").GetBytes(postStr);
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
webClient.Headers.Add("ContentLength", sendData.Length.ToString(CultureInfo.InvariantCulture));
var readData = webClient.UploadData(url, "POST", sendData);
result = Encoding.GetEncoding("GB2312").GetString(readData);
}
catch (Exception ex)
{
result = ex.Message;
}
return result;
}
///
/// Http Post
///
/// Url
/// Url
///
///
public static void HttpPostAsync(string url, string postStr = "",
UploadDataCompletedEventHandler callBackUploadDataCompleted = null, Encoding encode = null)
{
var webClient = new WebClient { Encoding = Encoding.UTF8 };
if (encode != null)
webClient.Encoding = encode;
var sendData = Encoding.GetEncoding("GB2312").GetBytes(postStr);
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
webClient.Headers.Add("ContentLength", sendData.Length.ToString(CultureInfo.InvariantCulture));
if (callBackUploadDataCompleted != null)
webClient.UploadDataCompleted += callBackUploadDataCompleted;
webClient.UploadDataAsync(new Uri(url), "POST", sendData);
}
}
}
2. 호출 클래스
using System;
using System.Net;
using System.Text;
using System.Web.UI;
namespace Test
{
public partial class WebForm3 : Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(HttpCommon.HttpGet("http://localhost:14954/WebForm4.aspx")); //Get
HttpCommon.HttpGetAsync("http://localhost:14954/WebForm4.aspx"); //Get
HttpCommon.HttpGetAsync("http://localhost:14954/WebForm4.aspx", DownStringCompleted); //Get
Response.Write(HttpCommon.HttpPost("http://localhost:14954/WebForm4.aspx", "post=POST")); //Post
HttpCommon.HttpPostAsync("http://localhost:14954/WebForm4.aspx", "post=POST"); //Post
HttpCommon.HttpPostAsync("http://localhost:14954/WebForm4.aspx", "post=POST", UploadDataCompleted); //Post
}
private void DownStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
Response.Write(e.Result);
}
private void UploadDataCompleted(object sender, UploadDataCompletedEventArgs e)
{
Response.Write(Encoding.GetEncoding("GB2312").GetString(e.Result));
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.