HttpWebRequest
How do I use Http Web Request
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: AccountService.cs
Copy
59 public void RegisterByEmail(string email, Origin origin)
60 {
61 this.registrationCallback = null;
62 this.AppId = string.Empty;
63 this.Message = string.Empty;
64 this.ReturnCode = -1;
65
66 string result;
67 try
68 {
69 WebRequest req = HttpWebRequest.Create(this.RegistrationUri(email, (byte)origin));
70 HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
71
72 // now read result
73 StreamReader reader = new StreamReader(resp.GetResponseStream());
74 result = reader.ReadToEnd();
75 }
76 catch (Exception ex)
77 {
78 this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
79 this.Exception = ex;
80 return;
81 }
82
83 this.ParseResult(result);
84 }
File name: AccountService.cs
Copy
93 public void RegisterByEmailAsync(string email, Origin origin, Action94 {
95 this.registrationCallback = callback;
96 this.AppId = string.Empty;
97 this.Message = string.Empty;
98 this.ReturnCode = -1;
99
100 try
101 {
102 HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(this.RegistrationUri(email, (byte)origin));
103 req.Timeout = 5000;
104 req.BeginGetResponse(this.OnRegisterByEmailCompleted, req);
105 }
106 catch (Exception ex)
107 {
108 this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
109 this.Exception = ex;
110 if (this.registrationCallback != null)
111 {
112 this.registrationCallback(this);
113 }
114 }
115 }
File name: AccountService.cs
Copy
121 private void OnRegisterByEmailCompleted(IAsyncResult ar)
122 {
123 try
124 {
125 HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
126 HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse;
127
128 if (response != null && response.StatusCode == HttpStatusCode.OK)
129 {
130 // no error. use the result
131 StreamReader reader = new StreamReader(response.GetResponseStream());
132 string result = reader.ReadToEnd();
133
134 this.ParseResult(result);
135 }
136 else
137 {
138 // a response but some error on server. show message
139 this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
140 }
141 }
142 catch (Exception ex)
143 {
144 // not even a response. show message
145 this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
146 this.Exception = ex;
147 }
148
149 if (this.registrationCallback != null)
150 {
151 this.registrationCallback(this);
152 }
153 }
HttpWebRequest 124 lượt xem
Gõ tìm kiếm nhanh...