![](/img/trans.png)
[英]c# HttpWebRequest.GetResponse() returned an error: (404) Not Found
[英]C# HttpWebRequest.GetResponse encoding URL
我正在使用C#HttpWebRequest.GetResponse和一個未編碼的URL。 但是當我使用fiddler檢查請求時,我發現URL已被編碼。 我想確認HttpWebRequest Class是在內部編碼URL但在文檔中找不到它。 有人能指出我可以找到這個或其他方式來驗證它的文檔嗎?
這里是:
WebRequest.Create
(您正在使用該方法創建HttpWebRequest對象,對吧?)說“Create方法使用requestUriString參數創建一個傳遞給新WebRequest的Uri實例”
https://msdn.microsoft.com/en-us/library/bw00b1dc(v=vs.110).aspx
Uri
的構造函數(由Create方法調用)表示它“解析URI, 將其置於規范格式,並進行任何必需的轉義編碼 ”
https://msdn.microsoft.com/en-us/library/z6c2z492(v=vs.110).aspx
//端點是url,方法可以發布或獲取...以下將幫助您從服務器捕獲任何錯誤..
使它成為一個功能,因此它可以很容易地在任何你想要的地方使用...
HttpWebRequest httpRequest = (HttpWebRequest)HttprequestObject(endpoints, method);
using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
{
streamWriter.Write(jsonObject);
streamWriter.Flush();
streamWriter.Close();
}
var result = "";
HttpWebResponse httpResponse;
try
{
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
}
catch (WebException e)
{
Console.WriteLine("This program is expected to throw WebException on successful run." +
"\n\nException Message :" + e.Message);
if (e.Status == WebExceptionStatus.ProtocolError)
{
Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
using (Stream data = e.Response.GetResponseStream())
using (var reader = new StreamReader(data))
{
string text = reader.ReadToEnd();
Console.WriteLine(text);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
或者你想要類,你想用來創建api lib然后只是復制粘貼這個類..這是為了c#但幾乎是java的similer語法...
有了這個你可以訪問任何api或url或用於將json文件傳遞給服務器..
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ExternalAPIs_SynapsePay.Helpers
{
class RESTHelpers
{
public dynamic APICalls(JObject jsonObject, string endpoints, string method)
{
HttpWebRequest httpRequest = (HttpWebRequest)HttprequestObject(endpoints, method);
using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
{
streamWriter.Write(jsonObject);
streamWriter.Flush();
streamWriter.Close();
}
var result = "";
HttpWebResponse httpResponse;
try
{
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
}
catch (WebException e)
{
Console.WriteLine("This program is expected to throw WebException on successful run." +
"\n\nException Message :" + e.Message);
if (e.Status == WebExceptionStatus.ProtocolError)
{
Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
using (Stream data = e.Response.GetResponseStream())
using (var reader = new StreamReader(data))
{
string text = reader.ReadToEnd();
Console.WriteLine(text);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return result;
//return "Success";
//not sure what to return
//here i have to add sql server code to enter into database
}
public HttpWebRequest HttprequestObject(string endpoints, string method)
{
string url = Setting.API_TEST_VALUE + endpoints;
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = method;
return httpWebRequest;
}
/// <summary>
/// This method is useful when you have to pass JSON as string not as object... This is basically a constructor..
/// Even if u pass json object it will accept, so no need to worry about that.
/// </summary>
/// <param name="ljson"></param>
/// <param name="endpoints"></param>
/// <param name="method"></param>
/// <returns></returns>
public dynamic APICalls(string jsonString, string endpoints, string method)
{
HttpWebRequest httpRequest = (HttpWebRequest)HttprequestObject(endpoints, method);
using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
{
streamWriter.Write(jsonString);
streamWriter.Flush();
streamWriter.Close();
}
var result = "";
HttpWebResponse httpResponse;
try
{
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
}
catch (WebException e)
{
Console.WriteLine("This program is expected to throw WebException on successful run." +
"\n\nException Message :" + e.Message);
if (e.Status == WebExceptionStatus.ProtocolError)
{
Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
using (Stream data = e.Response.GetResponseStream())
using (var reader = new StreamReader(data))
{
string text = reader.ReadToEnd();
Console.WriteLine(text);
result = text;
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return result;
//return "Success";
//not sure what to return
//here i have to add sql server code to enter into database
}
public void HttlpResponseObject(HttpWebRequest httpResponse)
{
var response = (HttpWebResponse)httpResponse.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
//entry into databse can be done from here
//or it should return some value
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.