khi tôi thực hiện GET với WebRequest.Create ("http://abc/test.") Tôi nhận được 404 vì theo fiddler dấu chấm bị tước đi bởi .NET và máy chủ web cần dấu chấm. làm thế nào tôi có thể ngăn chặn điều đó hoặc làm việc xung quanh nó. bất kỳ cách giải quyết nào đều được đánh giá cao!HttpWebRequest với URL có dấu chấm ở cuối
Trả lời
Đây là vấn đề đã được biết đến trên diễn đàn của Microsoft một vài lần.
Lớp Uri
không chính xác nghĩ rằng tất cả URI hoạt động như tệp đĩa Windows nơi dấu chấm (không có đuôi tệp) không liên quan.
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/5206beca-071f-485d-a2bd-657d635239c9/
Cách giải quyết trong tab workaround tại báo cáo lỗi chính thức:
.. có vẻ là hợp lệ. Về cơ bản, chạy mã này để đặt một lá cờ tĩnh trong .NET trước khi làm việc với System.Uri:
MethodInfo getSyntax = typeof(UriParser).GetMethod("GetSyntax", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
FieldInfo flagsField = typeof(UriParser).GetField("m_Flags", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (getSyntax != null && flagsField != null)
{
foreach (string scheme in new[] { "http", "https" })
{
UriParser parser = (UriParser)getSyntax.Invoke(null, new object[] { scheme });
if (parser != null)
{
int flagsValue = (int)flagsField.GetValue(parser);
// Clear the CanonicalizeAsFilePath attribute
if ((flagsValue & 0x1000000) != 0)
flagsField.SetValue(parser, flagsValue & ~0x1000000);
}
}
}
Chứng minh:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var surl = "http://x/y./z";
var url = new Uri(surl);
Console.WriteLine("Broken: " + url.ToString());
MethodInfo getSyntax = typeof(UriParser).GetMethod("GetSyntax", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
FieldInfo flagsField = typeof(UriParser).GetField("m_Flags", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (getSyntax != null && flagsField != null)
{
foreach (string scheme in new[] { "http", "https" })
{
UriParser parser = (UriParser)getSyntax.Invoke(null, new object[] { scheme });
if (parser != null)
{
int flagsValue = (int)flagsField.GetValue(parser);
// Clear the CanonicalizeAsFilePath attribute
if ((flagsValue & 0x1000000) != 0)
flagsField.SetValue(parser, flagsValue & ~0x1000000);
}
}
}
url = new Uri(surl);
Console.WriteLine("Fixed: " + url.ToString());
Console.WriteLine("Press ENTER to exit ...");
Console.ReadLine();
}
}
}
Thủ thuật thông minh. Hoạt động rất tốt. – harriyott
Cảm ơn bạn rất nhiều! ;-) –
không hoạt động đối với tôi. Tôi đang tạo một ứng dụng biểu mẫu cửa sổ. –
bạn thay đổi chấm vào String để Hex
string.format("{0:x2}",yoururl);
tôi nghĩ rằng nó hữu ích cho u, bởi vì tôi sử dụng nó trong twitter API Oauth định dạng
Viết lại một số của tôi t đến một chức năng không yêu cầu bạn thêm bất kỳ không gian tên nào
private Uri MyUri(string url)
{
Uri uri = new Uri(url);
System.Reflection.MethodInfo getSyntax = typeof(UriParser).GetMethod("GetSyntax", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
System.Reflection.FieldInfo flagsField = typeof(UriParser).GetField("m_Flags", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (getSyntax != null && flagsField != null)
{
foreach (string scheme in new[] { "http", "https" })
{
UriParser parser = (UriParser)getSyntax.Invoke(null, new object[] { scheme });
if (parser != null)
{
int flagsValue = (int)flagsField.GetValue(parser);
// Clear the CanonicalizeAsFilePath attribute
if ((flagsValue & 0x1000000) != 0)
flagsField.SetValue(parser, flagsValue & ~0x1000000);
}
}
}
uri = new Uri(url);
return uri;
}
http://msdn.microsoft.com/en-us/library/hh367887(v=VS.110).aspx xác nhận điều này đã được sửa. NET4.5. – EricLaw