本文整理汇总了C#中HttpWebRequest.GetResponse方法的典型用法代码示例。如果您正苦于以下问题:C# HttpWebRequest.GetResponse方法的具体用法?C# HttpWebRequest.GetResponse怎么用?C# HttpWebRequest.GetResponse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpWebRequest
的用法示例。
在下文中一共展示了HttpWebRequest.GetResponse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: getResponse
protected HttpWebResponse getResponse(HttpWebRequest request)
{
// execute the request
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
return response;
}
示例2: GetResponse
private string GetResponse(string url, ref HttpWebRequest request)
{
StringBuilder sb = new StringBuilder();
Stream resStream = null;
HttpWebResponse response = null;
byte[] buf = new byte[8192];
try
{
// execute the request
response = (HttpWebResponse)request.GetResponse();
// we will read data via the response stream
resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);
// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?
}
catch (Exception err)
{
String exc = err.Message;
}
finally
{
response.Close();
resStream.Close();
}
return sb.ToString();
}
示例3: getResponce
public static string getResponce(HttpWebRequest request)
{
try
{
HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(resp.GetResponseStream());
string result = reader.ReadToEnd();
reader.Close();
return result + "&status=200";
}
catch (Exception ex)
{
string statusCode = "";
if (ex.Message.Contains("403"))
statusCode = "403";
else if (ex.Message.Contains("401"))
statusCode = "401";
return string.Format("status={0}&error={1}", statusCode, ex.Message);
}
}
示例4: GET
public string GET(string url,WebProxy proxy=null)
{
LastOK = false;
string s = string.Empty;
req = (HttpWebRequest)WebRequest.Create(url);
req.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36";
req.Proxy = (WebProxy)proxy;
req.UseDefaultCredentials = false;
req.KeepAlive = false;
req.Timeout = 3000;
try
{
using (StreamReader sr = new StreamReader(req.GetResponse().GetResponseStream()))
{
LastOK = true;
return sr.ReadToEnd();
}
}
catch(Exception ex)
{
LastError = ex.Message;
return null;
}
}
示例5: getResponse
private static Boolean getResponse(HttpWebRequest request)
{
try
{
response = request.GetResponse() as HttpWebResponse;
return true;
}
catch (WebException e)
{
Console.WriteLine("Exception Message :" + e.Message);
if (e.Status == WebExceptionStatus.ProtocolError)
{
var response = ((HttpWebResponse)e.Response);
Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
var stream = response.GetResponseStream();
var reader = new StreamReader(stream);
var text = reader.ReadToEnd();
Console.WriteLine("Response Description : {0}", text);
}
return false;
}
}
示例6: execute
/// <summary>
/// Execute les requêtes et renvoie le JSON
/// </summary>
/// <param name="request">Destination de la requête</param>
private static JSONObject execute(HttpWebRequest request)
{
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream);
return JSONObject.Parse(reader.ReadToEnd());
}
}
catch (WebException ex)
{
Debug.Log(ex.Message);
JSONObject jsonError = new JSONObject();
var valueError = new KeyValuePair<string, JSONValue>("status", "error");
jsonError.Add(valueError);
return jsonError;
}
}
示例7: WebResponseGet
/// <summary>
/// Process the web response.
/// </summary>
/// <param name="webRequest">The request object.</param>
/// <returns>The response data.</returns>
public string WebResponseGet(HttpWebRequest webRequest)
{
StreamReader responseReader = null;
string responseData = "";
try
{
responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
responseData = responseReader.ReadToEnd();
}
catch
{
throw;
}
finally
{
webRequest.GetResponse().GetResponseStream().Close();
responseReader.Close();
responseReader = null;
}
return responseData;
}
示例8: WebResponseGet
/// <summary>
/// Process the web response.
/// </summary>
/// <param name="webRequest">The request object.</param>
/// <returns>The response data.</returns>
public string WebResponseGet(HttpWebRequest webRequest)
{
StreamReader responseReader = null;
string responseData = "";
try
{
responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
responseData = responseReader.ReadToEnd();
}
catch(Exception ex)
{
Global.CreateExceptionString(ex, null);
throw;
}
finally
{
webRequest.GetResponse().GetResponseStream().Close();
responseReader.Close();
responseReader = null;
}
return responseData;
}
示例9: Submit
public HttpWebResponse Submit (Uri uri, FSpot.ProgressItem progress_item)
{
this.Progress = progress_item;
Request = (HttpWebRequest) WebRequest.Create (uri);
CookieCollection cookie_collection = Cookies.GetCookies (uri);
if (uri.UserInfo != null && uri.UserInfo != String.Empty) {
NetworkCredential cred = new NetworkCredential ();
cred.GetCredential (uri, "basic");
CredentialCache credcache = new CredentialCache();
credcache.Add(uri, "basic", cred);
Request.PreAuthenticate = true;
Request.Credentials = credcache;
}
Request.ServicePoint.Expect100Continue = expect_continue;
Request.CookieContainer = new CookieContainer ();
foreach (Cookie c in cookie_collection) {
if (SuppressCookiePath)
Request.CookieContainer.Add (new Cookie (c.Name, c.Value));
else
Request.CookieContainer.Add (c);
}
Request.Method = "POST";
Request.Headers["Accept-Charset"] = "utf-8;";
//Request.UserAgent = "F-Spot Gallery Remote Client";
Request.UserAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7) Gecko/20040626 Firefox/0.9.1";
Request.Proxy = WebProxy.GetDefaultProxy ();
if (multipart) {
GenerateBoundary ();
Request.ContentType = "multipart/form-data; boundary=" + boundary;
Request.Timeout = Request.Timeout * 3;
long length = 0;
for (int i = 0; i < Items.Count; i++) {
FormItem item = (FormItem)Items[i];
length += MultipartLength (item);
}
length += end_boundary.Length + 2;
//Request.Headers["My-Content-Length"] = length.ToString ();
if (Buffer == false) {
Request.ContentLength = length;
Request.AllowWriteStreamBuffering = false;
}
} else {
Request.ContentType = "application/x-www-form-urlencoded";
}
stream_writer = new StreamWriter (Request.GetRequestStream ());
first_item = true;
for (int i = 0; i < Items.Count; i++) {
FormItem item = (FormItem)Items[i];
Write (item);
}
if (multipart)
stream_writer.Write (end_boundary + "\r\n");
stream_writer.Flush ();
stream_writer.Close ();
HttpWebResponse response;
try {
response = (HttpWebResponse) Request.GetResponse ();
//Console.WriteLine ("found {0} cookies", response.Cookies.Count);
foreach (Cookie c in response.Cookies) {
Cookies.Add (c);
}
} catch (WebException e) {
if (e.Status == WebExceptionStatus.ProtocolError
&& ((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.ExpectationFailed && expect_continue) {
e.Response.Close ();
expect_continue = false;
return Submit (uri, progress_item);
}
throw new WebException (Mono.Unix.Catalog.GetString ("Unhandled exception"), e);
}
return response;
}
示例10: StartProcess
public void StartProcess()
{
try
{
byte[] byteData = null;
request = (HttpWebRequest)WebRequest.Create(this.url);
request.CookieContainer = cc;
request.UserAgent = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) " +
"(compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
request.Timeout = requestTimeout;
if (this.sendData != null)
{
ASCIIEncoding encoding = new ASCIIEncoding();
byteData = System.Text.Encoding.UTF8.GetBytes(this.sendData);//encoding.GetBytes(this.sendData);
request.Method = "POST";
request.ContentLength = byteData.Length;
request.ContentType = "application/x-www-form-urlencoded";
Stream s = request.GetRequestStream();
s.Write(byteData, 0, byteData.Length);
s.Close();
}
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
receiveData = response.GetResponseStream();
}
catch (WebException we)
{
receiveData = we.Response.GetResponseStream();
}
//System.Windows.Forms.MessageBox.Show(response.Headers.Get("Set-Cookie"));
}
catch (Exception exc)
{
System.Windows.Forms.MessageBox.Show("Səhv baş verdi!\nİnternet bağlantınızın açıq olmağını dəqiqləşdirin.","Xəbərdarlıq",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);
}
}
示例11: GetResponse
public static XDocument GetResponse(HttpWebRequest request)
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
return XDocument.Load(new XmlTextReader(stream));
}
}
}
示例12: getResponse
public string getResponse(HttpWebRequest request)
{
WebResponse response = request.GetResponse();
Stream backStream = response.GetResponseStream();
StreamReader reader = new StreamReader(backStream);
string message = reader.ReadToEnd();
reader.Close(); response.Close(); backStream.Close();
return message;
}
示例13: HTTPPost
public HTTPPost(Uri Url, Dictionary<string, string> Parameters)
{
StringBuilder respBody = new StringBuilder();
request = (HttpWebRequest)HttpWebRequest.Create(Url);
request.UserAgent = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C10 Safari/419.3";
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string content = "?";
foreach (string k in Parameters.Keys)
{
content += k + "=" + Parameters[k] + "&";
}
content = content.TrimEnd(new char[] { '&' });
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(content);
request.ContentLength = byte1.Length;
byte[] buf = new byte[8192];
using (Stream rs = request.GetRequestStream())
{
rs.Write(byte1, 0, byte1.Length);
rs.Close();
response = (HttpWebResponse)request.GetResponse();
Stream respStream = response.GetResponseStream();
int count = 0;
do
{
count = respStream.Read(buf, 0, buf.Length);
if (count != 0)
respBody.Append(Encoding.ASCII.GetString(buf, 0, count));
}
while (count > 0);
respStream.Close();
ResponseBody = respBody.ToString();
EscapedBody = GetEscapedBody();
StatusCode = GetStatusLine();
Headers = GetHeaders();
response.Close();
}
}
示例14: WaitForResponse
/// <summary>
/// 接收网络的响应
/// </summary>
/// <param name="request"></param>
/// <param name="response">可能产生异常</param>
private static void WaitForResponse(HttpWebRequest request,HttpWebResponse response)
{
using(response=(HttpWebResponse )request .GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
#region 响应成功
using (Stream dataStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(dataStream))
{
string responseBody = reader.ReadToEnd();
try
{
OnObtainData(responseBody);
}
catch (Exception ex)
{
print("网络出现异常,异常信息:" + ex.Message);
}
}
}
#endregion
}
}
}
示例15: ProcessResponse
/// <summary>
/// Retrieves the response from the web request.
/// </summary>
/// <param name="request">The request from which to get the response.</param>
private void ProcessResponse(HttpWebRequest request)
{
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
{
string html = reader.ReadToEnd();
ParseQuote(html);
}
}
}
catch (WebException)
{
OnError();
}
}