HTTP
요청을 가진 매개 변수를 전달할 수 있습니까? 그렇다면 어떻게해야합니까? 게시물(link)을 발견했습니다. 이 예에서 문자열 postData
은 웹 서버로 전송됩니다. 나는 을 사용하여을 대신 사용하고 싶습니다. Google에서 HTTP
에 대한 예제를 발견하면 here을 얻습니다. 그러나 매개 변수는 웹 서버로 전송되지 않습니다.매개 변수로 HTTP get 요청을하는 방법
37
A
답변
17
GET 요청에서 매개 변수를 쿼리 문자열의 일부로 전달합니다.
string url = "http://somesite.com?var=12345";
85
처음으로 WebClient
은 사용하기가 쉽습니다. 인수가 쿼리 문자열에 지정된 GET - 유일한 트릭은 모든 값을 탈출 기억하는 것입니다
string address = string.Format(
"http://foobar/somepage?arg1={0}&arg2={1}",
Uri.EscapeDataString("escape me"),
Uri.EscapeDataString("& me !!"));
string text;
using (WebClient client = new WebClient())
{
text = client.DownloadString(address);
}
93
내 선호하는 방법이있다. 그것은 당신을 위해 이스케이프와 구문 분석을 처리합니다.
WebClient webClient = new WebClient();
webClient.QueryString.Add("param1", "value1");
webClient.QueryString.Add("param2", "value2");
string result = webClient.DownloadString("http://theurl.com");
+1
감사합니다, 훌륭한 답변입니다. –
6
WebRequest 객체는 저에게 너무 많은 작업처럼 보입니다. 나는 WebClient 컨트롤을 사용하는 것을 선호한다.
이 함수를 사용하려면 매개 변수를 보유하고 헤더를 요청하는 NameValueCollection을 두 개 만들어야합니다. (필요한 경우)
private static string DoGET(string URL,NameValueCollection QueryStringParameters = null, NameValueCollection RequestHeaders = null)
{
string ResponseText = null;
using (WebClient client = new WebClient())
{
try
{
if (RequestHeaders != null)
{
if (RequestHeaders.Count > 0)
{
foreach (string header in RequestHeaders.AllKeys)
client.Headers.Add(header, RequestHeaders[header]);
}
}
if (QueryStringParameters != null)
{
if (QueryStringParameters.Count > 0)
{
foreach (string parm in QueryStringParameters.AllKeys)
client.QueryString.Add(parm, QueryStringParameters[parm]);
}
}
byte[] ResponseBytes = client.DownloadData(URL);
ResponseText = Encoding.UTF8.GetString(ResponseBytes);
}
catch (WebException exception)
{
if (exception.Response != null)
{
var responseStream = exception.Response.GetResponseStream();
if (responseStream != null)
{
using (var reader = new StreamReader(responseStream))
{
Response.Write(reader.ReadToEnd());
}
}
}
}
}
return ResponseText;
}
이 당신의 쿼리 문자열 매개 변수를 추가하기 때문에 같은 NameValueCollection은 같은 :
는 다음과 같은 기능을 고려하십시오.
NameValueCollection QueryStringParameters = new NameValueCollection();
QueryStringParameters.Add("id", "123");
QueryStringParameters.Add("category", "A");
http 헤더 (필요한 경우)를 NameValueCollection으로 추가하십시오.
NameValueCollection RequestHttpHeaders = new NameValueCollection();
RequestHttpHeaders.Add("Authorization", "Basic bGF3c2912XBANzg5ITppc2ltCzEF");
0
URL을 통해 직접 값을 전달할 수도 있습니다.
당신이 방법을 다음 public static void calling(string name){....}
를 호출 할 경우에 당신은 그냥 당신이
당신이 ADRESSE의 매개 변수를 포함하여 전체 URL을 입력하면 URL에
?Object = value
을 사용하고 있는지 확인HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create("http://localhost:****/Report/calling?name=Priya); webrequest.Method = "GET"; webrequest.ContentType = "application/text";
를 사용하여 호출해야합니다 iexplore의 bar를 사용하면 HTTP 응답을 C에서 얻는 것과 같은 응답을 얻을 수 있습니까? – CruelIO그래야합니다. – EndangeredMassa
이것은 C# 코드가 아닙니다 ... – psyklopz