本文整理汇总了C#中System.Net.HttpWebResponse.Close方法的典型用法代码示例。如果您正苦于以下问题:C# HttpWebResponse.Close方法的具体用法?C# HttpWebResponse.Close怎么用?C# HttpWebResponse.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.HttpWebResponse
的用法示例。
在下文中一共展示了HttpWebResponse.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DisposeObject
private static void DisposeObject(ref HttpWebRequest request, ref HttpWebResponse response,
ref Stream responseStream, ref StreamReader reader)
{
if (request != null)
{
request = null;
}
if (response != null)
{
response.Close();
response = null;
}
if (responseStream != null)
{
responseStream.Close();
responseStream.Dispose();
responseStream = null;
}
if (reader != null)
{
reader.Close();
reader.Dispose();
reader = null;
}
}
示例2: ConnectionAvailable
public bool ConnectionAvailable()
{
try
{
req = (HttpWebRequest)WebRequest.Create("http://www.google.com");
resp = (HttpWebResponse)req.GetResponse();
if (HttpStatusCode.OK == resp.StatusCode)
{
// HTTP = 200 - Интернет безусловно есть!
resp.Close();
return true;
}
else
{
// сервер вернул отрицательный ответ, возможно что инета нет
resp.Close();
return false;
}
}
catch (WebException)
{
// Ошибка, значит интернета у нас нет. Плачем :'(
return false;
}
}
示例3: displayHttpWebReponse
/// <summary>
/// Outputs the HTTP reponse to the console
///
/// Code taken from http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx
/// </summary>
/// <param name="response"></param>
private static void displayHttpWebReponse(HttpWebResponse response)
{
Console.WriteLine(response.StatusCode);
Stream receiveStream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, encode);
Console.WriteLine("\r\nResponse stream received.");
Char[] read = new Char[256];
// Reads 256 characters at a time.
int count = readStream.Read(read, 0, 256);
Console.WriteLine("HTML...\r\n");
while (count > 0)
{
// Dumps the 256 characters on a string and displays the string to the console.
String str = new String(read, 0, count);
Console.Write(str);
count = readStream.Read(read, 0, 256);
}
Console.WriteLine("");
// Releases the resources of the response.
response.Close();
// Releases the resources of the Stream.
readStream.Close();
}
示例4: Login
public Login(string Username, string Password)
{
string url = string.Format("http://www.myfitnesspal.com/account/login");
request = (HttpWebRequest)WebRequest.Create(url);
request.AllowAutoRedirect = false;
request.CookieContainer = new CookieContainer();
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string NewValue = string.Format("username={0}&password={1}&remember_me=1",Username,Password);
request.ContentLength = NewValue.Length;
// Write the request
StreamWriter stOut = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(NewValue);
stOut.Close();
// Do the request to get the response
response = (HttpWebResponse)request.GetResponse();
StreamReader stIn = new StreamReader(response.GetResponseStream());
string strResponse = stIn.ReadToEnd();
stIn.Close();
cookies = request.CookieContainer;
response.Close();
}
示例5: WebResponse
public WebResponse(HttpWebRequest request, HttpWebResponse response)
{
_requestContentType = request == null ? null : request.ContentType;
_response = response;
_headers = new NameValueDictionary(response == null
? new System.Collections.Specialized.NameValueCollection()
: response.Headers);
_content = new MemoryStream();
if (_response != null)
{
using (var stream = _response.GetResponseStream())
{
stream.CopyTo(_content);
}
_content.Position = 0;
_response.Close();
}
#if !NET4
if (request.CookieContainer != null && response.Cookies.Count > 0)
{
request.CookieContainer.BugFix_CookieDomain();
}
#endif
}
示例6: GetResponseContent
public string GetResponseContent(HttpWebResponse response)
{
if (response == null)
{
throw new ArgumentNullException("response");
}
string responseFromServer = null;
try
{
// Get the stream containing content returned by the server.
using (Stream dataStream = response.GetResponseStream())
{
// Open the stream using a StreamReader for easy access.
using (StreamReader reader = new StreamReader(dataStream))
{
// Read the content.
responseFromServer = reader.ReadToEnd();
// Cleanup the streams and the response.
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
response.Close();
}
LastResponse = responseFromServer;
return responseFromServer;
}
示例7: GetResponseAsString
private static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
{
Stream stream = null;
StreamReader reader = null;
try {
// 以字符流的方式读取HTTP响应
stream = rsp.GetResponseStream();
switch (rsp.ContentEncoding) {
case "gzip":
stream = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress);
break;
case "deflate":
stream = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Decompress);
break;
}
reader = new StreamReader(stream, encoding);
return reader.ReadToEnd();
}
finally {
// 释放资源
if (reader != null) reader.Close();
if (stream != null) stream.Close();
if (rsp != null) rsp.Close();
}
}
示例8: Reporttoserver
public string Reporttoserver(string status, string testname, ref string error, string emailaddress)
{
vartestname = testname;
varstatus = status;
varerror = error;
count += 1;
if(status == "Complete" && emailaddress.Length > 5)
email(emailaddress);
string responsestring = ".";
try
{
req = (HttpWebRequest)WebRequest.Create("http://nz-hwlab-ws1:80/dashboard/update/?script=" + testname + "&status=" + status); //Complete
resp = (HttpWebResponse)req.GetResponse();
Stream istrm = resp.GetResponseStream();
int ch;
for (int ij = 1; ; ij++)
{
ch = istrm.ReadByte();
if (ch == -1) break;
responsestring += ((char)ch);
}
resp.Close();
//email();
return responsestring + " : from server";
}
catch (System.Net.WebException e)
{
error = e.Message;
System.Console.WriteLine(error);
string c = count.ToString();
return "ServerError " + c;
}
}
示例9: GetRemoteResponse
public static HttpRemoteResponse GetRemoteResponse(HttpWebResponse webResponse)
{
HttpRemoteResponse response = new HttpRemoteResponse(webResponse.StatusCode,
GetHeadersDictionaryFrom(webResponse.Headers),
GetContentFromStream(webResponse.GetResponseStream()));
webResponse.Close();
return response;
}
示例10: GetRawStringFromResponse
private string GetRawStringFromResponse(HttpWebResponse response)
{
using (var sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
var responseString = sr.ReadToEnd();
response.Close();
return responseString;
}
}
示例11: DisplayResponse
private static void DisplayResponse(HttpWebResponse hresp, string id)
{
Trace.WriteLine(null);
Trace.WriteLine("*** Response Start ***");
Trace.WriteLine(hresp.StatusCode);
Trace.WriteLine(hresp.StatusDescription);
DisplayHeaders(hresp.Headers);
DisplayContent(hresp, id);
hresp.Close();
Trace.WriteLine("*** Response End ***");
Trace.WriteLine("");
}
示例12: Response
public Response(HttpWebResponse response, string Format)
{
if (Format == "xml")
{
responseFormat = Format;
}
else {
responseFormat = "json";
}
setResponseBody(response);
response.Close();
}
示例13: printResponseToConsole
protected static void printResponseToConsole(HttpWebResponse response)
{
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
String responseXml = reader.ReadToEnd();
Console.Out.WriteLine("BlueTarp Auth status code: " + response.StatusCode);
Console.Out.WriteLine("BlueTarp Auth Response:");
Console.Out.WriteLine(responseXml);
dataStream.Close();
reader.Close();
response.Close();
}
示例14: deleteRecording
/*
* Method: deleteRecording()
* Summary: Attempts to delete a recording from the server
* Parameter: recJson - A recording string in the JSON format
* Returns: True or false depending on the success of the delete operation
*/
public static bool deleteRecording(string recJson)
{
// Prepare the DELETE HTTP request to the recordings endpoint
prepareRequest(recordingUrl, "text/json", "DELETE");
//RecordingManager recording = new RecordingManager(recJson);
bool result = false;
try
{
// Extract the ID from the recJson string
string recId = RecordingManager.getRecId(recJson);
// Create the JSON object to be written to the server
JObject recInfo = new JObject(
new JProperty("recId", recId),
new JProperty("UserId", Properties.Settings.Default.currentUser));
// Write the JSON string to the server
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(recInfo.ToString());
writer.Flush();
writer.Close();
}
try
{
// Retrieve the response
response = (HttpWebResponse)request.GetResponse();
// If the response code is "OK", return true
if (response.StatusCode == HttpStatusCode.OK)
result = true;
response.Close();
}
catch (WebException we)
{
Console.WriteLine(we.Message);
return false;
}
}
catch (WebException we)
{
Console.WriteLine(we.Message);
result = false;
}
return result;
}
示例15: GetContentForResponse
public static String GetContentForResponse(HttpWebResponse response)
{
Stream stream = response.GetResponseStream();
String characterSet = String.IsNullOrEmpty(response.CharacterSet) ? "UTF-8" : response.CharacterSet;
Encoding encoding = Encoding.GetEncoding(characterSet);
StreamReader reader = new StreamReader(stream, encoding);
String htmlContent = reader.ReadToEnd();
stream.Close();
response.Close();
return htmlContent;
}